Ansible - Change hostname

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 Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 9b242 in the box below so that we can be sure you are a human.