- Netomate
- Posts
- Ansible Mastery Part 5 :Dynamic Hostname Configuration with AnsibleNew Post
Ansible Mastery Part 5 :Dynamic Hostname Configuration with AnsibleNew Post
Giving Your Routers Real Names (the Easy Way)

Welcome back to our Ansible Mastery Journey. If you are new to here , don’t miss to check previous posts before diving into this post.
Ever stare at a bunch of IPs and think, "This would be so much easier if these routers had names?" Same. Instead of setting hostnames manually on each Cisco box, here’s a simple way to get Ansible to do it for you. No copy-pasting the same command again and again. Just clean automation.
Let’s get straight into it.
Start With Your Inventory
Open up your hosts.ini
file. Here's how I set mine up:
[anurudh@localhost ansible_start]$ cat hosts.ini
[routers]
1.1.1.1
2.2.2.2
[ios_devices:children]
routers
[ios_devices:vars]
ansible_user=cisco
ansible_password=cisco
ansible_connection=network_cli
ansible_network_os=ios[anurudh@localhost ansible_start]$
This tells Ansible: “Hey, we’re working with Cisco IOS gear, here are the devices, and here’s how to log in.”
Make Sure Ansible Knows What’s Up
Next, check your ansible.cfg
. You don’t need anything fancy — just make sure it points to your inventory file and uses the right connection method:
[anurudh@localhost ansible_start]$ cat ansible.cfg
[defaults]
gathering = explicit
inventory = hosts.ini
transport = network_cli
#host_key_checking = False # Disables SSH host key checking
[anurudh@localhost ansible_start]$
That commented-out line disables SSH key checking — helpful during testing, but use it wisely.
Now the Fun Part: Auto-Generate Hostnames
You don’t want to manually set hostnames like R1111
and R2222
, right? Let Ansible do that. Here's a tiny playbook that makes it happen:
[anurudh@localhost ansible_start]$ cat 04_config_hostname.yml
---
- name: Configure Hostane on Cisco routers
hosts: ios_devices
gather_facts: no
tasks:
- name: set hostname based on IP
cisco.ios.ios_config :
lines:
- hostname R{{ inventory_hostname | regex_replace('\\.', '') }}
[anurudh@localhost ansible_start]$
What’s happening here?
inventory_hostname
is the IP of each device.The
regex_replace('\\.', '')
bit strips out the dots.So
1.1.1.1
becomesR1111
. Neat, right?
Let’s See It in Action
Run the playbook like this:
[anurudh@localhost ansible_start]$ ansible-playbook 03_ping_router
PLAY [check connectivity to cisco routers] *************************************************
TASK [ping the router] *********************************************************************
ok: [1.1.1.1]
ok: [2.2.2.2]
PLAY RECAP *********************************************************************************
1.1.1.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.2.2.2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[anurudh@localhost ansible_start]$
Once it runs, SSH into your devices to confirm the hostname stuck:
[anurudh@localhost ansible_start]$ ssh [email protected]
([email protected]) Password:
R1.1.1.1#exit
Connection to 1.1.1.1 closed.
[anurudh@localhost ansible_start]$ ssh [email protected]
([email protected]) Password:
R2.2.2.2#
If you see that hostanme R111 for router 1 and R2222 for router 2, congrats — it worked.
Recap
You learned how to build hostnames from router IPs
You used Jinja2 to clean them up
You pushed config with Ansible
You did it without repeating yourself 50 times
If you’ve got more routers to name, this setup will save you hours. Add this to your playbook toolbox — you’ll use it again.
Action Step for You Today
Just one thing: make sure to go through the concepts and follow along with the tasks.
I’ll guide you, one simple post at a time.
We will deep dive more into Ansible in upcoming posts. Don’t miss to DM or ping me with your queries and comments.
Smiles :)
Anurudh
Reply