Reusable Assets in Ansible
- Ansible playbooks can be reused directly
- import_playbook module Refer Here
- include_playbook module Refer Here
- Import vs Include: Refer Here
- Skipping or including only certain tasks in the playbook can be done by tags Refer Here
- Refer Here for the playbook with tags
- Run the playbook
Ansible Roles and Collections
- Ansible hosts reusable roles in Ansible Galaxy Refer Here
- Lets try to use some role to install mysql Refer Here
- Lets get the role into the ansible control node
ansible-galaxy install <role-name>
ansible-galaxy install geerlingguy.mysql
* Lets write a playbook to use this role
---
- name: use existing role
become: yes
hosts: dbservers
roles:
- role: geerlingguy.mysql
- Lets create a ansible role to install tomcat
- Lets look at conents of the role skeleton
- In roles we have different folders
- defaults: for default variable values
- files: all the static files of the role
- templates: all the .j2 files containing templates
- tasks: all the tasks that need to be executed
- handlers: all the handlers
- vars: for variables
- meta: is metadata about role
- Refer Here for the role created and used in the classroom
Exercise
- Install nop commerce on a ubuntu server using the steps Refer Here
- Create an ansible playbook for deploying nop commerce
- Create a reusable role for deploying nop commerce
Ansible Parallelism
- Ansible parallelism can be set by using forks, the default number is 5.
- Ansible will create 5 parallel connections to 5 servers in inventory, execute the playbook tasks and once they are complete, then next batch of 5 servers start
- If we need to change the number of forks
ansible-playbook -f <count>
- Playbook execution strategies: Refer Here
- Ordering inventory strategies Refer Here
Dynamic inventory
- Create an executable in any programming language/shell script which will return the server ip address/fqdn i the json format suggested by ansible.
- Refer Here for aws based dynamic inventory.