 [](https://www.linkedin.com/in/nirgeier/) [](mailto:nirgeier@gmail.com) [](mailto:nirg@codewizard.co.il)
this project Lab 002 - No inventory example¶
01. “Clear” the inventory¶
- Lets clear the inventory from previous steps and walk thorough what is
inventory
01.01. Create the inventory file¶
Ansible Inventory¶
- An Ansible inventory is a file or collection of files that defines the
[hosts]and[groups]of hosts upon which Ansible will operates. - It’s simply a list of servers that Ansible can connect to and manage.
Key features of Ansible inventory¶
- Can be in various formats, with
INI,JSON,YAMLand more. YAMLis the most common formatinventorydefines the target hosts forplaybookexecutioninventoryorganizes hosts into logical groups for easier managementinventorycan store host-specific variables and group variables-
inventorysupports nested groups (groups of groups)inventorysamples¶INIformatYAMLformatJSONformat
inventory types in Ansible¶
-
Static Inventory
- This is generally a simple text file (usually in INI or YAML format) that lists your hosts and their corresponding groups.
-
Dynamic Inventory
- This is generated by a script or a program that retrieves host information from an external source, such as cloud providers (like
AWS,Azure, etc.),LDAP, or adatabase. -
This allows for real-time updates and adaptability as environments change.
- Example of generating
Dynamic inventoryusing python code fetching data form datable#!/usr/bin/env python import sqlite3 import json def get_inventory(): conn = sqlite3.connect('servers.db') cursor = conn.cursor() cursor.execute('SELECT hostname, group_name, ansible_user FROM servers') rows = cursor.fetchall() inventory = {'all': {'hosts': [], 'vars': {}}} for row in rows: hostname, group_name, ansible_user = row if group_name not in inventory: inventory[group_name] = {'hosts': [], 'vars': {}} inventory['all']['hosts'].append(hostname) inventory[group_name]['hosts'].append(hostname) inventory[group_name]['vars']['ansible_user'] = ansible_user conn.close() return inventory if __name__ == "__main__": print(json.dumps(get_inventory()))
- Example of generating
- This is generated by a script or a program that retrieves host information from an external source, such as cloud providers (like
Lab¶
-
Lets create the inventory configuration we will use for the labs:
01. No inventory invocation¶
-
Once all is ready lets check is the controller can connect to the servers with the using
ping# Ping the servers and check that they are "alive" docker exec ansible-controller sh -c "cd /labs-scripts && ansible all -m ping" ## Output ## ------------------------------------------------------------------------------- [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
02. inventory invocation¶
- Fill in the inventory based upon the prevoius labs configuration and test it.
- Verify that the inventory is defined correctly with:
- Test the inventory file with
-
Suggested Solution
### ### List of servers which we want ansible to connect to ### The names are defined in the docker-compose ### [servers] linux-server-1 ansible_ssh_common_args='-o UserKnownHostsFile=/root/.ssh/known_hosts' linux-server-2 ansible_ssh_common_args='-o UserKnownHostsFile=/root/.ssh/known_hosts' linux-server-3 ansible_ssh_common_args='-o UserKnownHostsFile=/root/.ssh/known_hosts'
:arrow_backward: /Labs/001-verify-ansible Back to labs list /Labs/003-modules :arrow_forward: