Ansible httpd php task

hey team,

I got this task failed, but the playbook worked just fine

it’s saying php is not installed, but all the packages are already there


here’s the playbook:

could you please take a look ?

But you wrote in the field of hosts: stapp03 ? Is this task for all app server? or you added wrong host?

hey @player001
it was listed in the task that the playbook only applies to stapp03 - NOT all hosts

I had a similar issue.

What is the actual task is? Can you share a image of that task?

See the task:

And my playbook:

the screenshot of task that @andybubune posted is right.

the playbook that I posted do the same and have the same final result as required by task.

It’s really crazy task. I has solved it. Checking script runs playbook again and ‘/var/www/html/myroot’ becomes ‘/var/www/html/myroot/myroot’

And stapp03 works not correct, you will not solve task on it, choose “Try later” for reload

cd ~/playbooks

cat << EOF > httpd.yml

  • name: Ansible Setup Httpd and PHP
    hosts: stapp02
    become: true
    • name: rem httpd
      name: httpd
      state: absent

    • name: install httpd
      name: httpd
      state: latest

    • name: rm php
      name: php
      state: latest

    • name: change document root

    • name: root dir check_mode
      path: /var/www/html/myroot
      register: isCreated

    • name: make root dir
      path: /var/www/html/myroot
      state: directory
      mode: 0755
      group: apache
      owner: apache
      when: isCreated.stat.exists == false

    • name: template copying
      src: ~/playbooks/templates/phpinfo.php.j2
      dest: /var/www/html/myroot/phpinfo.php
      group: apache
      mode: 0755
      owner: apache

    • name: start
      name: httpd
      state: restarted

    • name: apache enable
      name: httpd
      enabled: yes


Can someone help me understand which regexp to use which can correctly search the string .

- name: "Configure document root"
      path: /etc/httpd/conf/httpd.conf
      regexp: '/var/www/html.$'
      replace: '/var/www/html/myroot'

I used the above regexp which removed the " from DocumentRoot line and replaces it.
Need assistance as i am not good with string match reg expressions.

DocumentRoot "/var/www/html/myroot

Any help on the above regexp?

Thank you in advance!

            path: /etc/httpd/conf/httpd.conf
            regexp: "DocumentRoot /var/www/html"
            replace: "DocumentRoot /var/www/html/myroot"

It’s working fine. Try it.

Thank you for your reply :slight_smile:

Your solution will not work. I tried it too but failed because if you try to rerun the ansible playbook it will become.

DocumentRoot /var/www/html/myroot/myroot

Therefore it will ideally only work with the correct regexp string search.

Please use the below.

  • name: Change document root path in httpd.conf
    path: /etc/httpd/conf/httpd.conf
    regexp: ‘/var/www/html.$’
    replace: ‘/var/www/html/myroot"’
1 Like

hey there, use this because you want it to be idempotent. Tested and works
- name: “Change default document root of Apache”
path: “/etc/httpd/conf/httpd.conf”
regexp: ‘^DocumentRoot’
line: “DocumentRoot /var/www/html/myroot”

1 Like

Thanks @Chudo

Yes it needs to be idempotent. That’s why my config failed when repeatedly run.

I prefer actually lineinfile over replace:
My version:

  • name: change default document root for apache
    path: /etc/httpd/conf/httpd.conf
    regexp: '^DocumentRoot ’
    insertafter: '^#DocumentRoot ’
    line: ‘DocumentRoot “/var/www/html/myroot”’
1 Like

There is template module which in my opinion is better than copy:

  • name: Template a file
    src: “~/playbooks/templates/phpinfo.php.j2”
    dest: “/var/www/html/myroot/phpinfo.php”
    owner: apache
    group: apache
1 Like

@andrzej using template I am getting this error, any idea what could cause this issues?

fatal: [stapp03]: FAILED! => {“changed”: false, “msg”: “Could not find or access ‘~/playbooks/templates/phpinfo.php.j2’ on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option”}

@siraj_krm, you ran your playbook on stapp03, but the error says app server 1. Do the task description said app server 3?

@nashwan, do you have the template file phpinfo.php.j2 on jump server?