How to use custom roles
- Now Create a playbook to execute the roles
- Roles are generally referred from playbooks
- Roles will be checked in
- relative to playbook filepath (./roles/<role-name>)
- in the home directory (~/.ansible/roles/<role-name>)
- in the ansible cfg directory (/etc/ansible/roles/<role-name>)
- Roles will be checked in
- Refer here for the playbook scripts
Dynamic Inventory
- Refer here for official docs
- Dynamic inventory is a script written in any language which returs a json file in a particular structure when –hosts is called or when –list
- Sample Structure of json
{
'web': {
'hosts': ['192.168.10.11', '192.168.10.12'],
'vars': {
'package_name': 'httpd',
'ansible_ssh_user': 'ubuntu'
}
},
'db': {
'hosts': ['192.168.11.11', '192.168.11.12'],
'vars': {
'username': 'admin',
'password': 'admin@123'
}
}
}
- Example: Refer Here
Ansible Tower
-
Is an orchestration tool around ansible which can be used using
- Web Pages
- REST API
-
Ansible Tower is a paid tool
-
For the labsetup, we would use a Ansible Tower trail version
-
Ansible Tower Quick Start Guide Refer Here
-
Using Ansible Tower is all about configuring ansible-playbook executions from ui/periodic scheduled jobs and from rest api .
-
Ansible Tower will maintain logs of every run done by ansible.
Enterprise Scenario
- After writing Ansible Playbooks/Roles etc you need to run them from CI/CD Tools (like Jenkins/Azure DevOps) or from InfraProvisioning tools like Terraform/CloudFormation/ARM Templates
Exercise:
-
Write an Ansible Role to configure Tomcat 8 and then add playbooks to deploy gameoflife and openmrs into the Tomcat 8 Download Here
How to run the same ansible playbook for multiple environments
- Lets assume you have role called as tomcat8
- Lets assume you have a playbook called as useroledemo.yml
- Now lets assume we have 4 environments
- dev
- QA
- Automated-QA
- UAT
- Solution: For every environment create a separate inventory file
- dev_hosts =>
ansible-playbook -i env\dev_hosts useroledemo.yaml
- QA_hosts =>
ansible-playbook -i env\QA_hosts useroledemo.yaml
- Automated-QA_hosts =>
ansible-playbook -i env\Automated-QA_hosts useroledemo.yaml
- UAT_hosts=>
ansible-playbook -i env\UAT_hosts useroledemo.yaml
- dev_hosts =>
Can i run Ansible Playbook on 100 nodes at same time
- Ansible-PLaybook has option called as forking (-f), if you want to run ansible on 100 servers at same time use “`ansible-playbook -f 100 useroledemo.yml’
- To run ansible-playbook on one server at a time
ansible-playbook -f 1 useroledemo.yml
What is synchronize module
- To recursively copy file then use synchronize module Refer Here