Inventories
- Refer Here for official docs
- inventory contains list of servers/systems to connect to.
- Inventory can be written in two formats
- ini format (hosts)
- yaml format
- Inventory allows us to group servers
-
prompt:
I want to learn writing ansible inventories, Explain ini format -
by default ansible uses
allto represent all hosts in inventory file - basic inventory without groups. consider this example1
10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10

* in yaml
all:
hosts:
10.100.0.11:
10.100.0.12:
10.100.0.13:
10.100.0.14:
10.100.0.15:
10.100.0.16:
10.100.0.17:
10.100.0.18:
10.100.0.19:
10.100.0.10:
- consider this example2
[webservers]
10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16
[dbservers]
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10

* same in yaml
all:
children:
webservers:
hosts:
10.100.0.11:
10.100.0.12:
10.100.0.13:
10.100.0.14:
10.100.0.15:
10.100.0.16:
dbservers:
hosts:
10.100.0.17:
10.100.0.18:
10.100.0.19:
10.100.0.10:
- Example
[webservers]
www[01:06].example.com
- The above mentioned inventories are referred as static inventories.
-
Inventories can be dynamic, i.e. it points to
- an executable (Script) which generates a list of servers in a specific json format
- Ansible also has plugins to get servers from cloud providers like aws, azure, gcp
-
The default inventory file is in
/etc/ansible/hosts
Ansible debug messages
- Consider the simple playbook
---
- name: sample playbook
hosts: all
become: yes
tasks:
- name: update packages and install tree
ansible.builtin.apt:
name: tree
update_cache: yes
state: present
- Explicit debug messages using debug module
---
- name: sample playbook
hosts: all
become: yes
tasks:
- ansible.builtin.debug:
msg: "Installing tree package"
- name: update packages and install tree
ansible.builtin.apt:
name: tree
update_cache: yes
state: present
- ansible.builtin.debug:
msg: "Installed tree package"
- Ansible also supports printing verbose information prompt:
Ansible playbook verbose levels in a table
Bailout or failing ansible
- To fail ansible playbook we have a module called fail Refer Here
---
- name: sample bailout playbook
hosts: all
become: yes
tasks:
- ansible.builtin.fail:
msg: "Under development"
