Ansible - Change hostname
by
Jeremy Canfield |
Updated: August 18 2022
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The hostname module is used to update the hostname of managed nodes, such as updating the /etc/hostname file on the managed node, assuming the managed node is a Linux system. This module would typically be used with the -i or --limit command line options, so that only a single manage node is targeted. Also, the -e or --extra-vars command line option would probably be used to define the new hostname.
ansible-playbook foo.yml -i server1.example.com, --extra-vars "newhostname=server1"
Here is what you would probably have in your playbook.
---
- hosts: all
tasks:
- name: set new hostname to {{ newhostname }}
hostname:
name: "{{ newhostname }}"
Did you find this article helpful?
If so, consider buying me a coffee over at