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

image

here’s the playbook:

could you please take a look ?
thanks!

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

hey @Tej-Singh-Rana
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:

1 Like

@Tej-Singh-Rana
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
    tasks:
    • name: rem httpd
      yum:
      name: httpd
      state: absent

    • name: install httpd
      yum:
      name: httpd
      state: latest

    • name: rm php
      yum:
      name: php
      state: latest

    • name: change document root
      replace:
      path=/etc/httpd/conf/httpd.conf
      regexp=“/var/www/html”
      replace=“/var/www/html/myroot”

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

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

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

    • name: start
      service:
      name: httpd
      state: restarted

    • name: apache enable
      service:
      name: httpd
      enabled: yes

EOF

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

- name: "Configure document root"
    replace:
      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!

        replace:
            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
    replace:
    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”
lineinfile:
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
    lineinfile:
    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
    template:
    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?