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
What is the actual task is? Can you share a image of that task?
@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.
- name: Change document root path in httpd.conf
replace:
path: /etc/httpd/conf/httpd.conf
regexp: ‘/var/www/html.$’
replace: ‘/var/www/html/myroot"’
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:
- name: change default document root for apache
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^DocumentRoot ’
insertafter: '^#DocumentRoot ’
line: ‘DocumentRoot “/var/www/html/myroot”’
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
@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.