See the task:
And my playbook:
@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: absentname: install httpd
yum:
name: httpd
state: latestname: rm php
yum:
name: php
state: latestname: 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: isCreatedname: make root dir
file:
path: /var/www/html/myroot
state: directory
mode: 0755
group: apache
owner: apache
when: isCreated.stat.exists == falsename: template copying
copy:
src: ~/playbooks/templates/phpinfo.php.j2
dest: /var/www/html/myroot/phpinfo.php
group: apache
mode: 0755
owner: apachename: start
service:
name: httpd
state: restartedname: apache enable
service:
name: httpd
enabled: yesEOF
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
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.
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”
I prefer actually lineinfile over replace:
My version:
There is template module which in my opinion is better than copy:
@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?
@francilio yes I do have that file there! fixed it. by using
/home/thor/playbooks/templates/phpinfo.php.j2
instead of
~/playbooks/templates/phpinfo.php.j2
all good worked as expected.
Hello, nashwan
You are correct. We have to provide the absolute path.
The play book worked fine but the tak marked fail.
Bellow the script. Please advise :
name: “Install apache”
package: name=httpd state=present
name: “Install apache2-php5”
package: name=php state=present
name: change document root
replace:
path: /etc/httpd/conf/httpd.conf
regexp: “/var/www/html”
replace: “/var/www/html/myroot”
name: “make root dir”
file:
path: /var/www/html/myroot
state: directory
mode: ‘0755’
group: apache
owner: apache
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 Apache service”
service:
name: httpd
state: restarted
name: “apache enable”
service:
name: httpd
enabled: yes
Hello, chawki
What’s the failed message? Your replace module not looks fine. have you cross checked? It is replaced?
Here is your mistake: you should add DocumentRoot at the beginning of each regexp and replace
regexp: “DocumentRoot /var/www/html”