Ansible Playbook
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to configure Ansible dynamic inventory for AWS.
https://cloudaffaire.com/ansible-dynamic-inventory-for-aws/
In this blog post, we will discuss Ansible Playbook. We will also create our 1st Playbook.
What is Ansible Playbook?
In previous blog posts, we have executed Ansible commands on an ad-hoc basis which is great for testing. But ad-hoc commands are not designed for reuse. If you want a more standard approach of configuration management, you need to use Ansible Playbook.
Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a configuration state for your remote systems to deploy and maintain, or a set of steps in a general IT process. Each playbook is composed of one or more ‘plays’ in a list. The goal of a play is to map a group of hosts to some well-defined roles, represented by things ansible calls tasks. At a basic level, a task is nothing more than a call to an ansible module.
If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts are your raw material.
Next, we are going to create and deploy our 1st Playbook.
Ansible Playbook Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
##------------------------------------ ## Ansible Playbook: Introduction ## ##------------------------------------ ## Systems used for this demo # hostnames ip os role # --------- ------------ -------- ------------ # system1 192.168.0.10 Centos 7 Control Node # system2 192.168.0.20 Centos 7 Managed Node One # system3 192.168.0.30 Centos 7 Managed Node Two ## Create the inventory sudo vi /etc/ansible/hosts -------------------- [localserver] 192.168.0.10 [appserver] 192.168.0.20 [dbserver] 192.168.0.30 [remoteserver:children] appserver dbserver [systems:children] localserver remoteserver -------------------- :wq ## Create a playbook file mkdir my_playbook vi my_playbook/myplaybook1.yml -------------------------- --- - hosts: remoteserver remote_user: debjeet tasks: - name: test connection ping: ... -------------------------- :wq cd ## Varify playbook syntax sudo yum install epel-release -y sudo yum install python-pip -y sudo pip install ansible-lint ansible-lint myplaybook1.yml #will return error if any syntax error #or ansible-playbook my_playbook/myplaybook1.yml --syntax-check ## Execute the playbook ansible-playbook my_playbook/myplaybook1.yml ## next install httpd in appserver through playbook ## check if httpd already installed in system3 (192.168.0.30) ssh system2 sudo systemctl status httpd exit ## Create playbook file for httpd installation vi my_playbook/myplaybook2.yml -------------------------- --- - hosts: appserver remote_user: debjeet become: yes become_method: sudo tasks: - name: installs httpd yum: name: httpd state: latest - name: start httpd service service: name: httpd state: started ... -------------------------- :wq ## Execute the playbook ansible-playbook my_playbook/myplaybook2.yml ## Check if httpd installed in system2 (192.168.0.20) ssh system2 sudo systemctl status httpd exit ## Multiple play in a single playbook ## Create playbook file vi my_playbook/myplaybook3.yml -------------------------- --- - hosts: appserver remote_user: debjeet become: yes become_method: sudo tasks: - name: installs httpd yum: name: httpd state: latest - name: start httpd service service: name: httpd state: started - hosts: dbserver remote_user: debjeet become: yes become_method: sudo tasks: - name: installs mysql repo yum: name: http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state: latest - name: installs mysql yum: name: mysql-community-server state: latest - name: start mysqld service service: name: mysqld state: started ... -------------------------- :wq ## Execute the playbook ansible-playbook my_playbook/myplaybook3.yml ## Handlers, notify and listen ## Stops httpd using task and send a notify to handler to start httpd again ## Create playbook file vi my_playbook/myplaybook4.yml -------------------------- --- - hosts: appserver remote_user: debjeet become: yes become_method: sudo handlers: - name: start httpd service: name: httpd state: started listen: "httpd stopped" tasks: - name: enable httpd service: name: httpd enabled: yes - name: stop httpd service: name: httpd state: stopped notify: "httpd stopped" ... -------------------------- :wq ## Execute the playbook ansible-playbook my_playbook/myplaybook4.yml ## Cleanup ssh system2 sudo yum remove httpd -y exit ssh system3 sudo yum remove mysql -y exit rm -r my_playbook |
Hope you have enjoyed this article. In the next blog post, we will discuss Ansible Playbook.
To get more details on Ansible, please refer below Ansible documentation.