ansible-debian-bootstrap icon indicating copy to clipboard operation
ansible-debian-bootstrap copied to clipboard

Add user defined hosts and IP addresses to /etc/hosts

Open blmhemu opened this issue 4 years ago • 0 comments

Add a default variable to let users set other hosts. Currently, I am using the below code.

- name: Add hostname to /etc/hosts
  become: true
  lineinfile:
    dest: /etc/hosts
    regexp: "127[.]0[.]0[.]1.*"
    line: "127.0.0.1 localhost.localdomain localhost {{ ansible_hostname }} # added by ansible"
    state: present
    backup: yes

- name: Add Ansible inventory mappings to /etc/hosts
  become: true
  blockinfile:
    path: /etc/hosts
    block: |
      {% for host in groups['all'] %}
      {{ hostvars[host].ansible_host }} {{ host }}
      {% endfor %}

I am thinking of switching over to this role and would be great if this part can be handled in this role itself.

FYI, I understand that you guys use 127.0.1.1 for hostname. I'm fine with that approach too. Just need space for peer hosts.

blmhemu avatar Sep 20 '21 12:09 blmhemu