Sunday, 5 February 2023

Ansible – Playbooks

Ansible – Playbooks
Ansible – Role
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.

Creating a New Role
  
The directory structure for roles is essential to create a new role.
Role Structure
Roles have a structured layout on the file system. The default structure can be changed but for now let us stick to defaults.
Each role is a directory tree in itself. The role name is the directory name within the /roles directory.

$ ansible-galaxy -h Usage ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [-- help] [options] ...

 Options 

 -h, --help − Show this help message and exit.

  -v, --verbose − Verbose mode (-vvv for more, -vvvv to enable connection debugging) 

 --version − Show program's version number and exit. 

Creating a Role Directory The above command has created the role directories. 

$ ansible-galaxy init Emamrole


ERROR! The API server (https://galaxy.ansible.com/api/) is not responding, please try again later.
 $ ansible-galaxy init --force --offline Emamrole
 - Emamrole was created successfully
 $ tree Emamrole/ 

 Emamrole/ 

 ├── defaults │ └── main.yml 

 ├── files ├── handlers 

 │ └── main.yml 

 ├── meta │ └── main.yml 

 ├── README.md ├── tasks │ 

└── main.yml ├── templates ├── tests │ ├── inventory 

 │ └── test.yml 

 └── vars 

 └── main.yml 

 8 directories, 8 files

 Not all the directories will be used in the example and we will show the use of some of them in the example

Utilizing Roles in Playbook

 This is the code of the playbook we have written for demo purpose. 

This code is of the playbook Emam_orchestrate.yml.

 We have defined the hosts: tomcat-node and called the two roles – installtomcat and start-tomcat. 

The problem statement is that we have a war which we need to deploy on a machine via Ansible.
--- - hosts: tomcat-node 

 roles:

 - {role: install-tomcat} 

 - {role: start-tomcat} 

Contents of our directory structure from where we are running the playbook. 

$ ls

 ansible.cfg hosts roles Emam_orchestrate.retry Emam_orchestrate.yml 

There is a tasks directory under each directory and it contains a main.yml.

 The main.yml contents of install-tomcat are −

 --- #Install Emam artifacts 

 - block: 

 - name: Install Tomcat artifacts 

 action: >

 yum name = "demo-tomcat-1" state = present 

 register: Output 

 always: - debug: msg: - "Install Tomcat artifacts task ended with message: {{Output}}"

 - "Installed Tomcat artifacts - {{Output.changed}}"


The contents of main.yml of the start tomcat are 

− #Start Tomcat 

 - block:

 - name: Start Tomcat 

 command: /bin/startup.sh" 

 register: output 

 become: true

 always: - debug: 

 msg: - "Start Tomcat task ended with message: {{output}}"

 15 - "Tomcat started - {{output.changed}}" 

 The advantage of breaking the playbook into roles is that anyone who wants to use the Install tomcat feature can call the Install Tomcat role. 

Hit the following URL and you will be directed to a page as shown below − http://10.70.0.136:11677/HelloWorld/HelloWorld
 The deployed war just has a servlet which displays “Hello World”.
The detailed output shows the time taken by each and every task because of the entry added in ansible.cfg file 

− [defaults] 

 callback_whitelist = profile_tasks

Ansible - Variables

Variable in playbooks are very similar to using variables in any programming language. It helps you to use and assign a value to a variable and use that anywhere in the playbook. One can put conditions around the value of the variables and accordingly use them in the playbook

Exception Handling in Playbooks

Exception handling in Ansible is similar to exception handling in any programming language. An 

example of the exception handling in playbook is shown below.

tasks: 

 - name: Name of the task to be executed 

 block: 

 - debug: msg = 'Just a debug message , relevant for logging' 

 - command: <the command to execute> 

 

 rescue: 

 - debug: msg = 'There was an exception.. ' 

 - command: <Rescue mechanism for the above exception occurred) 

  always: 

 - debug: msg = "this will execute in all scenarios. Always will get 

logged" 

Following is the syntax for exception handling.


Loops in Ansible.

Below is the example to demonstrate the usage of Loops in Ansible. The tasks is to copy the set of all the war files from one directory to tomcat webapps folder. Most of the commands used in the example below are already covered before. Here, we will concentrate on the usage of loops.

To loop, the 'with_items' syntax is being used. with_items: "{{output.stdout_lines}}" --> output.stdout_lines gives us the line by line output and then we loop on the output with the with_items command of Ansible. Attaching the example output just to make one understand how we used the stdout_lines in the with_items command. 

--- #Testing 

 - hosts: tomcat-node 

 tasks: - name: Install Apache 

 shell: "ls *.war" 

 register: output

 args: chdir: /opt/ansible/tomcat/demo/webapps

 - file: src: '/opt/ansible/tomcat/demo/webapps/{{ item }}' 

 dest: '/users/demo/Emam/{{ item }}' 

 state: link 

 with_items: "{{output.stdout_lines}}" 

Ansible - Advanced Execution

we will learn what is advanced execution with Ansible. 

How to Limit Execution by Tasks? 

This is a very important execution strategy where one needs to execute only one execution and not the entire playbook. For example, suppose you only want to stop a server 

(in case a production issue comes) and then post applying a patch you would like to only start the server. Here in original playbook stop and start were a part of different roles in the same playbook but this can be handled with the usage of tags. We can provide different tags to different roles 

(which in turn will have tasks) and hence based on the tags provided by the executor only that specified role/task gets executed. So for the above example provided, we can add tags like the following

 − - {role: start-tomcat, tags: ['install']}} 

 The following command helps in using tags 

− ansible-playbook -i hosts --tags "install" -vvv 

With the above command, only the start-tomcat role will be called. 

The tag provided is case-sensitive. Ensure exact match is being passed to the command.

Common Playbook Issues
 

we will learn about the a few common playbook issues. 

The issues are − 

 Quoting

  Indentation Playbook is written in yaml format and the above two are the most common issues in yaml/playbook.

 Yaml does not support tab based indentation and supports space based indentation, 

so one needs to be careful about the same. 

Note − once you are done with writing the yaml ,
goto this site(https://editor.swagger.io/) and copy paste your yaml on the left hand side to ensure that the yaml compiles properly.
This is just a tip. Swagger qualifies errors in warning as well as error & issue if any.................

Resources
https://www.ansible.com/ 

https://docs.ansible.com/ansible/latest/index.html 

https://en.wikipedia.org/wiki/Ansible_(software) 

https://github.com/ansible/ansible https://opensource.com/resources/what-ansible https://www.simplilearn.com/tutorials/ansible-tutorial/what-is-ansible 

https://www.guru99.com/ansible-tutorial.html 

https://hackr.io/tutorials/learn-ansible


 

No comments:

Post a Comment

How to Install Kubernetes How to Install Kubernetes? What is a Pod? previously , I discussed on what is Kubernetes, and its architecture. He...