Skip to content

Instantly share code, notes, and snippets.

@phips
Last active August 2, 2021 19:59
Show Gist options
  • Save phips/11233502 to your computer and use it in GitHub Desktop.
Save phips/11233502 to your computer and use it in GitHub Desktop.
Ansible setting of hostname from inventory, but ignoring IP addresses
- name: Ensure hostname set
hostname:
name: {{ inventory_hostname }}
when: not inventory_hostname|trim is match('(\d{1,3}\.){3}\d{1,3}')
- name: Ensure hostname is in /etc/hosts
lineinfile:
dest: /etc/hosts
regexp: "^{{ ansible_default_ipv4.address }}.+$"
line: "{{ ansible_default_ipv4.address }} {{ ansible_fqdn }} {{ ansible_hostname }}"
@phips
Copy link
Author

phips commented Dec 1, 2020


  • name: inventory name to hostname
    hostname:
    name: "{{ inventory_hostname }}"

Indeed @patsevanton — the original gist is in a really old format. You've correctly put the way things should be now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment