Ansible playbooks

For install and configure ansible with multiple nodes please read the “Ansible install configure and use” article.

Look at hostnames on webservers group nodes:
root@ansmaster:~# ansible -a “/bin/hostnamectl –static” webservers
node3ans.unixmen.com | success | rc=0 >>
node3ans.unixmen.com

node2ans.unixmen.com | success | rc=0 >>
node2ans.unixmen.com

Our task is install and configure wordpress on multiple nodes. For that we will use playbooks. The playbooks is simple text files written with YAML format and have one/many data list(we can understand this as “hash” or “dictionary“).

On every content of playbook we can find one or many host groups (Each of this groups calling with play name).

Official documentation give us the following details:

  1. hosts: This is the list of machines(Each of them must be in the master server /etc/ansible/hosts file) which can do the following works.
  1. remote_user: Remote Account for execution tasks.
  1. vars: Variables which we will use on remote machines.
  1. Fited hosts, tasks will be in order. With play all hosts will get the same task directives.

If you want to do different task for selected host, create other play in exists play (In other words, play must map tasks for selected hosts).

For example Play structure will be as following. Firstly coming host directives:

– hosts: webservers
remote_user: root
vars:
variable1: value1
variable2: value2
remote_user: root
tasks:
– name: description for task1
task1: parameter1=value_for_parameter1 parameter2=value_for_parameter2
– name: description for task1
task2: parameter1=value_for_parameter1 parameter2=value_for_parameter2
handlers:
– name: description for handler 1
service: name=name_of_service state=service_status
– hosts: dbservers
remote_user: root
vars:
variable1: value1
variable2: value2

  1. handlers – This is job in end of task section for all plays and most of the time uses for reboot remote machine or restart the services.

The following steps we will do on ansmaster.unixmen.com server

Create folder for playbooks
root@ansmaster:~# mkdir /etc/ansible/playbooks

Create /etc/ansible/playbooks/apache.yml file and add the following lines:

– hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
– name: ensure apache is at the latest version
yum: pkg=httpd state=latest
– name: replace default index.html file
copy: src=/static_files/index.html dest=/var/www/html/ mode=0644
notify:
– restart apache
– name: ensure apache is running (and enable it at boot)
service: name=httpd state=started enabled=yes
handlers:
– name: restart apache
service: name=httpd state=restarted

Create /static_files folder and index.html file.
root@ansmaster:~# mkdir /static_files ; touch /static_files/index.html

Content of the /static_files/index.html file will be as following:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″/>
</script>
</head>
<body>
<h1> Apache WEB installed and configured from the ansible master server</h1><br>
<h2>Provided by http://unixmen.com</h2>
</body>
</html>
As we talked before we must start the playbook. Please note, ansible will do every task step by step and will report status of every task:
root@ansmaster:~# ansible-playbook /etc/ansible/playbooks/apache.yml
PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [ensure apache is at the latest version] ********************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [replace default index.html file] ***************************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [ensure apache is running (and enable it at boot)] **********************
changed: [node2ans.unixmen.com]
changed: [node3ans.unixmen.com]

NOTIFIED: [restart apache] ****************************************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

PLAY RECAP ********************************************************************
node2ans.unixmen.com     : ok=5    changed=4    unreachable=0    failed=0
node3ans.unixmen.com     : ok=5    changed=4    unreachable=0    failed=0

Open the web browser for each of node:
playbook-site1
playbook-site2

For test purpose stop the apache web server on node1ans and node2ans nodes:
# systemctl stop httpd
# systemctl disable httpd
# systemctl is-enabled httpd

Execute the apache.yml again in the ansmaster.
root@ansmaster:~# ansible-playbook /etc/ansible/playbooks/apache.yml
PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [node2ans.unixmen.com]
ok: [node3ans.unixmen.com]

TASK: [ensure apache is at the latest version] ********************************
ok: [node2ans.unixmen.com]
ok: [node3ans.unixmen.com]

TASK: [replace default index.html file] ***************************************
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [ensure apache is running (and enable it at boot)] **********************
changed: [node2ans.unixmen.com]
changed: [node3ans.unixmen.com]

PLAY RECAP ********************************************************************
node2ans.unixmen.com     : ok=4    changed=1    unreachable=0    failed=0
node3ans.unixmen.com     : ok=4    changed=1    unreachable=0    failed=0