Skip to content

Instantly share code, notes, and snippets.

@rothgar
Last active March 8, 2024 07:16
Show Gist options
  • Save rothgar/8793800 to your computer and use it in GitHub Desktop.
Save rothgar/8793800 to your computer and use it in GitHub Desktop.
Generate /etc/hosts with Ansible
# Idempotent way to build a /etc/hosts file with Ansible using your Ansible hosts inventory for a source.
# Will include all hosts the playbook is run on.
# Inspired from http://xmeblog.blogspot.com/2013/06/ansible-dynamicaly-update-etchosts.html
- name: "Build hosts file"
lineinfile: dest=/etc/hosts regexp='.*{{ item }}$' line="{{ hostvars[item].ansible_default_ipv4.address }} {{item}}" state=present
when: hostvars[item].ansible_default_ipv4.address is defined
with_items: groups['all']
@iamenr0s
Copy link

iamenr0s commented Jun 26, 2020

@dywanik After some research, I could confirm there's no way to manipulate /etc/hosts with Molecule using docker platform. The /etc/hosts file is crucial for Docker's linking system and it should only be manipulated manually at the image level, rather than the container level.
See:
https://docs.docker.com/network/links/#updating-the-etchosts-file

William-Yeh/docker-ansible#4 (comment)

@iamenr0s
Copy link

Anyway the solution proposed here works fine.
I have simply added this code in my molecule.yml

...
provisioner:
  name: ansible
  inventory:
    group_vars:
      all:
        run_not_in_container: False
...

and then modified my tasks/main.yml

- name: Generate /etc/hosts file
  template:
    src: etc/hosts.j2
    dest: /etc/hosts
    owner: root
    group: root
    mode: 0644
  when: run_not_in_container

when you test your role remember to set the variable

[all:vars]
run_not_in_container=True

@gardar
Copy link

gardar commented Aug 5, 2021

@enr0s, you can also use the tag/variable molecule-notest that's already set by molecule, so you don't have to define your own.

tasks/main.yml

---
- name: Generate /etc/hosts file
  template:
    src: etc/hosts.j2
    dest: /etc/hosts
    owner: root
    group: root
    mode: 0644
  tags:
    - molecule-notest

or
tasks/main.yml

---
- name: Generate /etc/hosts file
  template:
    src: etc/hosts.j2
    dest: /etc/hosts
    owner: root
    group: root
    mode: 0644
  when: molecule-notest not in ansible_skip_tags

Another solution to this issue would be to use a "dummy" hosts file with molecule, which is not a perfect solution but is better than skipping the task in my opinion because you can then confirm that the hosts file is generated correctly.
The way to achieve that would be something like:

defaults/main.yml

---
etc_hosts_file: /etc/hosts

molecule.yml

provisioner:
  name: ansible
  inventory:
    group_vars:
      all:
        etc_hosts_file: /tmp/molecule_etc_hosts

tasks/main.yml

---
- name: Generate /etc/hosts file
  template:
    src: etc/hosts.j2
    dest: "{{ etc_hosts_file }}"
    owner: root
    group: root
    mode: 0644

@irigon
Copy link

irigon commented Dec 9, 2021

@dywanik , I am not sure if that is what you are searching for... in my case, I wanted simply to add an entry to /etc/hosts.
The following worked for me:

molecule.yml:

---
...
platforms:
  - name: "myplatformname"
    etc_hosts:
      "repo": "8.8.8.8"

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