Ansible Parallelism
- Parallelism can be set in Ansible using an argument –fork.
- Default value is 5
- Scenario:
- 100 nodes are in inventory and I run
ansible-playbook test.yml
, this execution happens in 20 batches - 100 nodes are in inventory and I run
ansible-playbook -f 50 test.yml
, this execution happens in 2 batches - 100 nodes are in inventory and I run
ansible-playbook -f 500 test.yml
, this execution happens in 1 batch
- 100 nodes are in inventory and I run
Ansible custom facts
- I have a playbook, inside this playbook I need to decide on some module execution depending on other module execution.
- Eg: only when you create a file, then only restart service and do further executions
- In any case if you want to store the value only till your playbook execution is completed, you can use a module called as set_fact
- For more info Refer Here
How to make non idempotent modules idempotent
- To make your non idempotent modules idempotent create some flags
- One easier implementation is create and check for file existence
- You can also try environmental variables
Register Variables
---
- hosts: all
become: yes
tasks:
- name: contents of file
command: cat /home/ansible/Readme.txt
register: readme_output
- name: display contents
debug:
var: readme_output
Vault
- Configuration Management has to deal with passwords or sensitive contents
- We need a mechanism to encrypt the sensitive content and thats exactly what ansible-vault does.
- Refer Here
- Execution in ansible requires vault password or password file to passed
ansible-playbook -i hosts --vault-password-file /var/lib/password test.yaml
Ansible Tower
-
Tower provides authentication and reporting features to ansible control server
-
Tower is web based platform for doing what you have done with ACS (Command line), we would have an user interface and REST API.
-
Terms
- Projects => Where your ansible playbooks are?
- Inventory
- Job
- Schedule Jobs
- Job Template
-
In this series we will use Ansible Trail Version on AWS
Dynamic Inventory
- If the node list is changing consistently, then how to handle this in inventory.
- Dynamic Inventory can be used.
- Dynamic Inventory is any script in any language which returns information about your nodes in json format specified by Ansible.
- Json structure should look like
{
"group001": {
"hosts": ["host001", "host002"],
"vars": {
"var1": true
},
"children": ["group002"]
},
"group002": {
"hosts": ["host003","host004"],
"vars": {
"var2": 500
},
"children":[]
}
}
- Refer Here for sample AWS scenario.
DevOps Scenarios in Ansible
- Consider the below Architecture
- To create this deployment, we can go with three roles
- tomcat => tomcat on servers
- mysql => mysql on server
- redis => redis on server
- Then you write your playbook => contoso.yml
---
- hosts: appservers
become: yes
roles:
- role: tomcat_deploy
- hosts: dbservers
become: yes
roles:
- role: mysql_deploy
- hosts: cacheservers
become: yes
roles
- role: redis_deploy
Realizing this with CI/CD
- Servers are already available with OS installed.
- Create a inventory with servers
- Create a Jenkins Pipeline/Freestyle Project with following steps
- Get latest code from GIT
- Build the code
- Call ansible-playbook to deploy the configuration
Realizing this with CI/CD and Infra Provisioning
- Starting Point : Servers are not available
- Create a Jenkins Pipeline/Freestyle Project with following steps
- Get latest code from GIT
- Build the code
- Call Terraform to create servers and then terraform calls ansible to do configuration management
Exercise
- Write ansible-playbook to deploy any of the sample application from here
- Try to make java -jar spring-petclinic.jar as a Linux service Refer Here