ANSIBLE
1. What is Ansible – confifuration management tool
2. Explain components of ansible architecture
Inventory, Playbook(yaml file), module, control machine
3. Explain playbook, play & task, module
4. Default location of Inventory File: /etc/ansible/hosts
5. Ansible ad-hoc command –single statement to complete a particular task
ansible host01 -i myhosts -m copy -a "src=[Link] dest=/tmp/"
6. Create Directories and Files
7. You can use file module to create files and directories, manage their permissions and ownership
as shown:
ansible host01 -i myhosts -m file -a "dest=/tmp/test mode=644 state=directory"
8. Default location for Ansible modules is /usr/share/ansible
9. What is ansible template
10. Exercise
Yaml file
#This is how you write in Playbook
- hosts: all
vars:
quarter: false,
salary : 30000,
extra : 10000,
names: ["John","David"]
tasks:
- name: Ansible Template
template:
src: ../templates/sample-template.j2
dest: /home/sample-template
j2 file
{% for name in names %}
Hi {{name}}!
{% if quarter -%}
Your pay cheque for this month is {{ salary + extra }}.
{% else -%}
Your pay cheque for this month is {{ salary }}.
{%- endif %}
{% endfor %}
11. Exercise
Try to make a template to display Students mark sheet that displays name of student, marks in
Physics, Chemistry and Maths.
Provide relevant values via variables.
12. Exercise
Sample playbook
---
- name: this play displays "hello world"
hosts: all
tasks:
- name: displaying "hello world"
shell: echo "hello world"
- name: displaying wishes for the day
shell: echo "have a good day"
…
To run playbook - ansible-playbook -i myhosts [Link]
13. Exercise
Install Apache
---
- name: install apache
hosts: all
sudo: yes
tasks:
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
14. Explain handlers
15. Sample playbook on conditions & loops
This Playbook will add Java Packages to different systems (handling Ubuntu/Debian OS)
- name: debian | ubuntu | add java ppa repo
apt_repository:
repo=ppa:webupd8team/java
state=present
become: yes
when: ansible_distribution == 'Ubuntu'
- name: debian | ensure the webupd8 launchpad apt repository is present
apt_repository:
repo="{{ item }} [Link] trusty main"
update_cache=yes
state=present
with_items:
- deb
- deb-src
become: yes
when: ansible_distribution == 'Debian'
16. What is [Link]
17. Ansible Is Idempotent
Try executing the same playbook for the second time and observe the output. Thus, Ansible is
Idempotent.
An Idempotent operation results to the same output, if executed multiple times (no changes).
18. Types of variables – playbook, inventory, complex, facts, built-in, registered variables
19. Exercise: use of register variable
---
- name: check registered variable for emptiness
hosts: all
tasks:
- name: list contents of the directory in the host
command: ls /home/ubuntu
register: contents
- name: check dir is empty
debug: msg="Directory is empty"
when: [Link] == ""
- name: check dir has contents
debug: msg="Directory is not empty"
when: [Link] != ""
20. Variables precedence - Command Line > Playbook > Facts > Roles
21. Explain tags
---
- name: Play1-install apache
hosts: all
sudo: yes
tasks:
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
- name: displaying "hello world"
debug: msg="hello world"
tags:
- tag1
- name: Play2-install nginx
hosts: all
sudo: yes
tags:
- tag2
tasks:
- name: install nginx
apt: name=nginx update_cache=yes state=latest
- name: debug module displays message in control machine
debug: msg="have a good day"
tags:
- mymessage
- name: shell module displays message in host machine.
shell: echo "yet another task"
tags:
- mymessage
You may save the above Playbook with name [Link] and run the following commands
ansible-playbook -i myhosts [Link] --list-tasks : displays the list of tasks in the Playbook
ansible-playbook -i myhosts [Link] --list-tags : displays only tags in your Playbook
ansible-playbook -i myhosts [Link] --tags "tag1,mymessage" : executes only certain tasks which
are tagged as tag1 and mymessage
22. Explain include keyword
---
- name: testing includes
hosts: all
sudo: yes
tasks:
- include: [Link]
- include: [Link]
- include: create_folder.yml
- include: [Link]
- include: [Link]
---
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
- name: displaying message
debug: msg="you are awesome!!"
---
- name: installing nginx
hosts: all
sudo: yes
tasks:
- name: install nginx
apt: name=nginx update_cache=yes state=latest
- name: displaying message
debug: msg="yayy!! nginx installed"
23. Explain Ansible roles
Ansible_cfg
[Link]
Roles
Sample_role
Files
Some_file.txt
Handlers
[Link]
Tasks
[Link]
[Link]
Copy_static.yml
Templates
Template.j2
Vars
[Link]
24. What is ansible galaxy
ansible-galaxy init sample_role
25. What are emvironment variables & how it is used