Host variables can be used to manage host at a another level by defining the variable or a name to define what you want it to do. Here I will create a new host_vars folder, create a new yml file with the servers ip address, in this case the variable i will use are for the webserver. The handler will allow another yml file to run using the notify option when there is a change.
let get started and create the host_vars folder and my webserver ip address yaml file. You can see the variable here in bold
cd /etc/ansible
sudo mkdir host_vars
cd host_vars
sudo nano 192.168.1.128.yml
apache_package_name: apache2
apache_service: apache2
php_package_name: libapache2-mod-php
#apache_package_name: httpd
#apache_service: httpd
#php_package_name: php
now I will modify the webserver main.yml file
sudo nano /etc/ansible/roles/webservers/tasks/main.yml
here you will see the where the variable is being used and the notify option which restarts the service if there is a change. I have put it in bold
- name: install httpd package (CentOS)
tags: apache,centos,httpd
dnf:
name:
- httpd
- php
state: latest
when: ansible_distribution == "CentOS"
- name: start and enable httpd (CentOS)
tags: apache,centos,httpd
service:
name: httpd
state: started
enabled: yes
when: ansible_distribution == "CentOS"
- name: install apache2 package (Ubuntu)
tags: apache,apache2,ubuntu
apt:
name:
- '{{ apache_package_name }}'
- '{{ php_package_name }}'
state: latest
when: ansible_distribution == "Ubuntu"
- name: start apache server
service:
name: '{{ apache_service }}'
state: started
enabled: yes
when: ansible_distribution == 'Ubuntu'
- name: change line in file
lineinfile:
path: /etc/ansible/playbooks/testfile.txt
regexp: '^4.random5'
line: changethisline ok please
when: ansible_distribution == 'Ubuntu'
notify: restart_apache
Now lets create the handlers folder and main.yml in it this will reference the notify: restart_apache
cd /etc/ansible/roles/webservers/
mkdir handlers
cd handlers
sudo nano main.yml
You can see the names are matching in the two yml files, notify: restart_apache and – name: restart_apache
- name: restart_apache
tags: apache,centos,httpd
service:
name: '{{ apache_service }}'
state: restarted
When a change in the line occurs the apache service will be restart
ansible-playbook /etc/ansible/playbooks/everything.yml --ask-become-pass
