Operations 6 min read

Using Ansible Playbook to Set Hostnames and Update /etc/hosts on Multiple Servers

This article demonstrates how to create and run an Ansible Playbook that changes hostnames and appends entries to /etc/hosts on two Linux servers, showing the required inventory, YAML playbook, command execution, and verification steps with accompanying screenshots.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using Ansible Playbook to Set Hostnames and Update /etc/hosts on Multiple Servers

Playbook Introduction

An Ansible Playbook is a more powerful and flexible alternative to using the Ansible command line directly. It is a simple configuration‑management and multi‑host deployment system described in YAML, allowing ordered execution of tasks synchronously or asynchronously.

1. Hostnames of the two servers

Server 192.168.20.40:

[root@docker02 ~]# hostname
docker02

Server 192.168.20.39:

[root@slavedb tmp]# hostname
slavedb

2. Ansible configuration

# /etc/ansible/hosts
[web]
k8s-master ansible_ssh_host=192.168.20.40
k8s-node3  ansible_ssh_host=192.168.20.39

3. Playbook configuration (host.yml)

---
- hosts: web
tasks:
- name: hostname
shell: hostnamectl set-hostname {{ inventory_hostname }}
when: ansible_distribution_major_version == "7"

4. Execute the playbook

[root@docker02 ~]# ansible-playbook host.yml

Verify hostnames after execution:

[root@docker02 ~]# hostname
k8s-master
[root@slavedb tmp]# hostname
k8s-node3

5. Write host entries into /etc/hosts

On server 192.168.20.40:

[root@docker02 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.40 docker02
192.168.20.39 slavedb

On server 192.168.20.39 the same entries appear.

6. Extend the playbook to modify /etc/hosts

- name: modify etc hosts
shell: echo "{{ ansible_ens33['ipv4']['address'] }} {{ inventory_hostname }}" >>/etc/hosts
register: result
- debug: var=result
[root@docker02 ~]# ansible-playbook host.yml

7. Verify the updated /etc/hosts

[root@docker02 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.40 docker02
192.168.20.39 slavedb
192.168.20.40 k8s-master
[root@slavedb tmp]# cat /etc/hosts | egrep -v "^$|^#" /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.40 docker02
192.168.20.39 slavedb
192.168.20.39 k8s-node3

Related reading

Write Playbook to Deploy an Etcd Cluster

Playbook for Bulk Docker Deployment

Playbook to Distribute Nginx Configuration Files

automationConfiguration ManagementDevOpsAnsibleplaybook
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.