Skip to content

Instantly share code, notes, and snippets.

@antonioaguilar
Forked from phips/hosts.yml
Created April 17, 2018 20:42
Show Gist options
  • Save antonioaguilar/78bfaefe0596c1b4308ab4c8b75ce535 to your computer and use it in GitHub Desktop.
Save antonioaguilar/78bfaefe0596c1b4308ab4c8b75ce535 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|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 }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment