Ansible Chors Content
Ansible Chors Content
---
- hosts: all
vars:
var: 20
tasks:
Variables In Playbook
Variables may also be included in a separate file, using the vars_files section
file: playbook.yml
---
- hosts: all
vars_files:
- vars.yml
tasks:
file: vars.yml
---
var: 20
You can create the above two files in Katacoda and run it: ansible-playbook -i
myhosts playbook.yml
Variables In Inventory
You can add variables in your inventory files as:
[group1]
host1 http_port=80
host2 http_port=303
#group variables
[group1:vars]
ntp_server= example.com
proxy=proxy.example.com
/etc/ansible/host_vars/host1.example.com
/etc/ansible/group_vars/group1.yml
/etc/ansible/group_vars/webservers
---
hosts: all
vars:
foo:
- one
- two
- three
tasks:
For example:
consider you want to retrieve only IPv4 address of the server. But you
might first prefer to take a look at the structure of the entire built-in variable
(ansible_eth0) as shown:
Go ahead and try this playbook in Katacoda
---
hosts: all
tasks:
- debug: var=ansible_eth0
Facts
You might have observed this output while running the playbook.
GATHERING FACTS *************************
ok: [host01]
**Whenever you run Playbook, Ansible by default collects information (facts) about each
host like host IP address, CPU type, disk space, operating system information etc.**
Run the following in Katacoda and observe if the task (Gathering Facts) runs or
not:
---
hosts: all
gather_facts: no
tasks:
- action: ping
Built-In Variables
In Ansible you get some variables by default, even if you do not define
them, that helps you to access information about other hosts. Thus you
should not define these variables explicitly, as they are reserved.
hostvars
groups
inventory_hostname
Hostvars
Occasionally you might be running a task that needs the value of the
variable defined in other hosts. By default, variables in Ansible are
scoped by hosts.
Ansible provides you with hostvars variable that has all the variables and
facts (facts are gathered only if you have talked to the host at least once in any
play of your playbook) of all the hosts.
For Example:
Consider you need to run a task in host1 that need IP address of the eth1
interface of the host2
{{ hostvars['host2'].ansible_eth1.ipv4.address }}
Hostvars
Occasionally you might be running a task that needs the value of the
variable defined in other hosts. By default, variables in Ansible are
scoped by hosts.
Ansible provides you with hostvars variable that has all the variables and
facts (facts are gathered only if you have talked to the host at least once in any
play of your playbook) of all the hosts.
For Example:
Consider you need to run a task in host1 that need IP address of the eth1
interface of the host2
{{ hostvars['host2'].ansible_eth1.ipv4.address }}
[group1]
host1 http_port=80
host2 http_port=303
host3
#group variables
[group1:vars]
ntp_server= example.com
proxy=proxy.example.com
Groups
You can find the list of group names defined in your inventory
file using groups variable.
For Example: Consider you need the IP address of all the servers in
you web group
{% for host in groups.web %}
{% endfor %}
Inventory_hostname
You can find the name of the current host using inventory_hostname.
#inventory file
[group1]
server1 ansible_ssh_host=192.169.67.34
Syntax: register: variable_name
Let us now see how the return value of command module looks like. Go ahead
and run the following playbook in Katacoda
file: test.yml
---
hosts: all
tasks:
register: output
- debug: var=output
Registered Variable
This playbook will check the contents of the home directory of your host
machine (host01)and display a message accordingly
---
hosts: all
tasks:
command: ls /home/ubuntu
register: contents
Variable Precedence
If you define the same variable in multiple places, it will be overwritten in a certain
order as shown:
Command Line > Playbook > Facts > Roles
#file: test.yml
---
- hosts: all
vars:
ansible_bios_version: Fresco
tasks:
CLI: While running the playbook in Command Line redefine the variable
- ansible-playbook -i myhosts test.yml --extra-vars
"ansible_bios_version=Ansible"
Tags
Tags arenames pinned on individual tasks, roles or an entire play, that allows
you to run or skip parts of your Playbook.
Tags can help you while testing certain parts of your Playbook.
file: tag.yml
---
hosts: all
sudo: yes
tasks:
tags:
- tag1
sudo: yes
tags:
- tag2
tasks:
tags:
- mymessage
tags:
- mymessage
Running Tag.yml
You may save the above Playbook with name tag.yml and run the following
commands in Katacoda
Special Tags
Ansible has some special keywords for tags:
untagged: run only those tags which do not have any tags
Include
Till now you were dumping all the tasks and other stuff (handlers, variables) in a
single Playbook. This makes it cumbersome to maintain the Playbook as it grows.
Ansible gives you the flexibility of organizing your tasks through include keyword,
that introduces more abstraction and make your Playbook more easily maintainable,
reusable and powerful.
playbook_include.yml
---
hosts: all
sudo: yes
tasks:
- include: apache.yml
- include: content.yml
- include: create_folder.yml
- include: content.yml
- include: nginx.yml
You might need root access to install anything in host, that's why sudo: yes
apache.yml
---
create_folder.yml
---
content.yml
---
command: ls /home/ubuntu
register: contents
nginx.yml
---
hosts: all
sudo: yes
tasks:
Did you notice something? If you did, you are awesome. And if you don't,
you are like 99.9999% others. Cheers!
nginx.yml, unlike apache.yml, is a Playbook which you can run independently. That
is why it has tasks and other keywords mentioned explicitly.
Intro To Roles
Roles are added abstraction of building your Playbook in more modular fashion,
where you hide all the technicalities by splitting your tasks into smaller files and
grouping them under respective folders of tasks, templates, handlers, vars etc.
Files
Handlers
Meta
Templates
Vars
Defaults
file: tasks/nginx.yml
---
notify:
- start nginx
file: handlers/main.yml
---
file: tasks/main.yml
---
- include: nginx.yml
file: master_playbook.yml
---
hosts: all
sudo: yes
roles:
- sample_role
Telling Ansible About Your Roles
You called your role in Playbook but how will Ansible know, where your roles are
defined?
file: ansible.cfg
[defaults]
host_key_checking=False
roles_path = /home/scrapbook/tutorial/roles/
```
What Is Happening?
So, let us have a look at what exactly is happening:
- .....
roles:
- sample_role
- sample_role2
....
file: tasks/copy-static.yml
---
file: tasks/main.yml
---
- include: nginx.yml
- include: copy-static.yml
master_playbook.yml
file: templates/template-file.j2
this is {{item}}
file: vars/main.yml
var_x:
- 'variable x'
var_y:
- 'variable y'
file: tasks/copy-template.yml
---
template:
src: template-file.j2
dest: /home/ubuntu/copy-template-file.j2
with_items: var_x
Phase 3 - Including Task in Main Task File
Include the task you just created in your main file:
file: tasks/main.yml
---
- include: nginx.yml
- include: copy-static.yml
- include: copy-template.yml
master_playbook.yml
Running Role
Let us run the master_playbook and check the output:
Environment Variables
Managing huge number of hosts can be tedious when the deployment
environment is different: development, staging and production
For example, memory requirement for servers in production might be
different from servers in the development environment
Ansible recommended way of solving this issue is by maintaining inventory file
for each environment, instead of keeping all your hosts in a single inventory.