It is a file, define the collection of hosts (remote system) and group of hosts upon which Ansible commands , module, playbook operates.
Types of inventory :
Static Inventory: A simple list of hosts and groups defined in a file (usually in INI or YAML format)
inventory.ini [inventory file , you can write it using yml format also]
[web]
10.10.10.10
[database]
192.168.0.2
or
[servers]
server_1 ansible_host=18.205.233.146
[servers:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3 ansible_ssh_private_key_file=/home/soumen321/pem-key/test-new.pem
Dynamic Inventory: It is generated by a script or external programs that dynamically retrieve the list of hosts from external sources, whaere the hosts constantly changing (e.g cloud environment)
Using a pythone script and aws boto3 module , get the running hosts (ec2 instances i.e manage node) , list of host ips, ans store it in a inventory file.
Example in github.
Ansible inventories are fundamental for managing the scope of automation tasks. Whether using a static inventory for a small, stable set of hosts or a dynamic inventory for more complex, fluid environments, understanding and properly configuring your inventory is crucial for effective automation with Ansible.
Adhoc Commands UseCase
Ping all hosts:
ansible -i inventory.ini -m ping all
Gather Facts from All Hosts
ansible all -m setup
Install a Package
ansible -i inventory.ini -m shell -a "sudo apt install openjdk-17-jre-headless -y" all
ansible web -m yum -a "name=httpd state=present"
Start a Service
ansible web -m service -a "name=httpd state=started"
Copy a File to All Hosts
ansible web -m copy -a "src=/path/to/local/file dest=/path/to/remote/file"
Execute a Command on All Hosts
ansible web -a "uptime"
Create a User
ansible web -m user -a "name=johndoe state=present"
Github :
https://github.com/soumen321/ansible-iac.git
Special Thanks to Abhishek Veeramalla
Youtube : https://www.youtube.com/Abhishek Veeramalla