Ansible Playbook Components
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Ansible Playbook.
https://cloudaffaire.com/ansible-playbook/
In this blog post, we will discuss Ansible Playbook components.
Playbook Components:
- Hosts: Using hosts, you can choose the target infrastructure where you want to execute the playbook. Hosts are defined in your inventory file and called in your playbook using hosts directive. You can use multiple hosts in the same playbook and each host can have a different task.
- Users: Using remote_user (formerly called just user), you define user name that will be used to execute your task in the host. The scope of the remote_user can be entire playbook or an individual task depending upon the placement of remote_user block. remote_user also supports running tasks as another user using become directive.
- Variables: Variables are a key component of the Ansible design. Variables allow for dynamic play content and reusable plays across different sets of inventories. Variables can be declared in inventory, playbook, included files and roles. There are other places (for example facts) where variables can come from, but these are a type of variable that is discovered, not set by the user.
- Tasks: Using tasks, you define the action or play that will be performed in the target host. Each play contains a list of tasks. Tasks are executed in order, one at a time, against all machines matched by the host pattern. Every task should have a name, which is included in the output from running the playbook. Every task should have a name, which is included in the output from running the playbook. Tasks can be declared using the legacy action: module options format, but it is recommended that you use the more conventional module: options format. Ansible supports lots of modules that you can call in your play.
- Handlers, Notify and Listen: Handlers are lists of tasks, not really any different from regular tasks, that are referenced by a globally unique name, and are notified by notifiers. If nothing notifies a handler, it will not run. Regardless of how many tasks notify a handler, it will run only once, after all of the tasks completed in a particular play. Handlers uses Listen method to listen to a notification sent by Notify.
- Roles: Roles are ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure which allows easy sharing of roles with other users. Roles expect files to be in certain directory names (for example tasks) and must include at least one of these directories with a main.yml file, which contains the relevant content.
Playbook order of execution:
- Variable loading
- Fact gathering
- The pre_tasks execution
- Handlers notified from the pre_tasks execution
- Roles execution
- Tasks execution
- Handlers notified from roles or tasks execution
- The post_tasks execution
- Handlers notified from post_tasks execution
Let us explain the order of execution and playbook components through a demo.
Ansible Playbook Component 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 |
##-------------------------------- ## Ansible Playbook: Components ## ##-------------------------------- ## 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 ## Login to the control node (192.168.0.10) ## Create the inventory sudo vi /etc/ansible/hosts -------------------- [localserver] 192.168.0.10 [appserver] 192.168.0.20 [dbserver] 192.168.0.30 -------------------- :wq ## Create Role named myapp mkdir -p myapp/tasks vi myapp/tasks/main.yml ------------------- - name: my_role_task debug: msg="Message from Task of Role" ------------------- :wq ## Create a playbook file vi myplaybook.yml -------------------- --- - hosts: appserver gather_facts: true remote_user: debjeet become: yes become_method: sudo vars: favcolor: blue handlers: - name: my_handler debug: var=favcolor listen: "Hello Handler" pre_tasks: - name: my_pre_task debug: msg="Message from Pre Task" roles: - role: myapp tasks: - name: my_task debug: msg="Message from Task" notify: "Hello Task Handler" post_tasks: - name: change color set_fact: favcolor: Orange - name: my_post_task debug: msg="Message from Post Task" changed_when: true notify: "Hello Handler" ... ---------------------- :wq ## Check playbook syntax ansible-playbook myplaybook.yml --syntax-check ## Execute the playbook ansible-playbook myplaybook.yml |
Hope you have enjoyed this article. We will explore each component in details in its separate blog post. In the next blog post, we will discuss variables in Ansible.
To get more details on Ansible, please refer below Ansible documentation.