BuggyCodeMaster

Back

Hello,

Here are some basic tips that can help you manage your inventory better. I also recommend looking at this Udemy course which is where I learned from.

Sample Inventory File#

This sample inventory file will be used in the forthcoming sections:

# web servers
web_node1 ansible_host=web01.mine.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node2 ansible_host=web02.mine.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node3 ansible_host=web03.mine.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
# db servers
sql_db1 ansible_host=sql01.mine.com ansible_connection=ssh ansible_user=root
sql_db2 ansible_host=sql02.mine.com ansible_connection=ssh ansible_user=root
ini

1. Check Connectivity to All Hosts#

Checking connectivity to all hosts is definitely something that may need to be run from time to time, just to make sure that the ssh/winrm daemons are working on the target servers or to just make sure they are not destroyed!

The ansible_connection config decides whether port 22 (ssh) or port 5986 (winrm) should be used for Linux and Windows hosts respectively.

You can check ssh or winrm connectivity by running the following command:

# pings all sql* hosts in inventory
ansible sql* -m ping -i inventory.txt

# pings all hosts in inventory, even though there is no group called 'all'
ansible all -m ping -i inventory.txt
bash

This uses the ping module explicitly without writing a playbook.

2. Single Line ‘Play-Books’#

You might have to write play-books to restart all hosts or to run a single command — well not really, you can just use a single ansible command instead of writing a playbook if all you are running is just one command.

# reboot all hosts, you can use regex as well
ansible all -a "/sbin/reboot" -i inventory.txt

# this is similar to the section 1
ansible db* -m ping -i inventory.txt
bash

You also have other modules that you can run — Ansible Module Reference

3. Group-ception#

In Ansible, you can also make groups of groups in an inventory like so:

# Web Servers
web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass
web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass

# DB Servers
sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass
sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass

# Groups
[db_nodes]
sql_db1
sql_db2

[web_nodes]
web_node1
web_node2
web_node3

[boston_nodes]
sql_db1
web_node1

[dallas_nodes]
sql_db2
web_node2
web_node3

[us_nodes:children]
boston_nodes
dallas_nodes
ini

This will allow you to just run commands on us_nodes host group just as you would reference any other group.

I hope this was useful, do let me know if you have any feedback — thanks.


This article was originally published by me on Medium.

Ansible Basics
https://sanjaybalaji.dev/blog/ansible-basics
Author Sanjay Balaji
Published at January 25, 2022