Ansible Tips

Sanjay Balaji
2 min readJan 5, 2022

Hello,

Here are some basic tips that can help you manage your inventory better. I also recommend looking at https://www.udemy.com/course/learn-ansible course which is where I learnt from.

Sample Inventory file that will be used in the forthcoming sections

#snippet_01
# 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

1. Check connectivity to all hosts on your inventory

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. [refer snippet_01]

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

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

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

You also have other modules that you can run — https://docs.ansible.com/ansible/2.8/modules/list_of_all_modules.html

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

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.

--

--

Sanjay Balaji

Master of buggy code. Explorer of the internet. Cloud DevOps Engineer.