Playbooks to run multiple, complex tasks against a set of targeted hosts in an easily repeatable manner.
A play is an ordered set of tasks which should be run against hosts selected from your inventory.
A playbook is a text file that contains a list of one or more plays to run in order.
Format of an Ansible Playbook
name: just an example
hosts: webservers
tasks:
- first
- second
- third
Example play has three keys: name, hosts, and tasks. These keys all have the same indentation because they belong to the play.
Example shows the contents of a simple playbook, and then the result of running it.
Note : client machine is where we will install web server
[rootcontrolnode ]$ mkdir imp-playdemo
[rootcontrolnode imp-playdemo]$ vim webserver.yml
name: play to setup web server
hosts: client-machine.example.com
tasks:
name: latest httpd version installed
yum:
name: httpd
state: latest
[root@controlnode imp-playdemo]$ ansible-playbook webserver.yml
PLAY [play to setup web server] ************************************************
TASK [Gathering Facts] *********************************************************
ok: [servera.lab.example.com]
TASK [latest httpd version installed] ******************************************
changed: [servera.lab.example.com]
PLAY RECAP *********************************************************************
servera.lab.example.com
: ok=2
changed=1
unreachable=0
failed=0
Syntax Verification : The ansible-playbook command offers a --syntax-check option which can be used to verify the syntax of a playbook file.
[root@controlnode ~]$ ansible-playbook --syntax-check webserver.yml
playbook: webserver.yml
Executing a Dry Run : Another helpful option is the -C option.
Note
: This causes Ansible to report what changes would have occurred if the
playbook were executed, but does not make any actual changes to managed
hosts.
[root@controlnode ~]$ ansible-playbook -C webserver.yml
Example : Install Apache package on client machine
A working directory, /home/root/basic-playbook, has been created on control server.
Step 1 :Need two server
- I . Host server (client machine) IP- 192.168.103.101
- II. Control Node
First check client machine is ping from Control machine.
[rootcontrol-node]$ping client.example.com
[root@control]$ mkdir inventory_dir
[root@control]$ cd inventory_dir
[root@control inventory_dir]$ vim inventory
[webserver]
192.168.103.101
Step 2: Configure ansible config file.
[root@control ~]$ mkdir /home/root/playbook
[root@control ~]$ cd /home/root/playbook
[root@control palybook]$ vim ansible.cfg
[defaults]
inventory=inventory
[rootcontrol-node]$cd /home/root/basic-playbook
[rootcontrol-node playbook]$vim site.yml
name: Install and start Apache HTTPD
hosts: web
tasks:
- name: httpd package is present
yum:
name: httpd
state: present
- name: correct index.html is present
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: httpd is started
service:
name: httpd
state: started
enabled: true
Save the file and exit your text editor.
TASK [Gathering Facts] *********************************************************
ok: [serverd.lab.example.com]
ok: [serverc.lab.example.com]
TASK [httpd package is present] ************************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]
TASK [correct index.html is present] *******************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]
TASK [httpd is started] ********************************************************
changed: [serverd.lab.example.com]
changed: [serverc.lab.example.com]
PLAY RECAP *********************************************************************
serverc.lab.example.com
: ok=4
changed=3
unreachable=0
failed=0
serverd.lab.example.com
: ok=4
changed=3
unreachable=0
failed=0
Implementing Multiple Plays
[root@control]vim playbook_two.yml
name: first play
hosts: web.example.com
tasks:
- name: first task
yum:
name: httpd
status: present
- name: second task
service:
name: httpd
enabled: true
-
name: second play
hosts: database.example.com
tasks:
- name: first task
service:
name: mariadb
enabled: true
No comments:
Post a Comment