0

How to Remove /etc/hosts entries on the remote server using Ansible playbook. I need only the default entries to be present.

I need only below entries to be present:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

To Be Removed Ex:

192.104.1.1     app.original.as.domain apporiginal  
kenlukas
  • 3,101
  • 2
  • 16
  • 26
celcoprab
  • 43
  • 2
  • 3
  • 8

2 Answers2

1

Probably the easiest way would be to rename the hosts file to a backup and copy a new one in it's place. You should be able to use the "copy" module to do both in one shot.

An example from the "copy" module doc page: https://docs.ansible.com/ansible/latest/modules/copy_module.html

- name: Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
  copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: '0644'
    backup: yes
serverguy
  • 11
  • 3
0

The simplest way to achieve that is probably deploying a minimal file; either by copying a literal file or by using a template, where the latter gives you the most flexibility, of course.

Mikael H
  • 5,031
  • 2
  • 9
  • 18
  • Could you help me with the template and code – celcoprab Apr 08 '20 at 09:32
  • See @serverguy's answer. For your specific problem, the copy module will do fine. Templating is almost exactly the same (search the web for "ansible template module" and you're likely to end up on the official documentation page (https://docs.ansible.com/ansible/latest/modules/template_module.html)). With templates you get the additional ability to insert variables into text files using jinja2 notation (" {{ variable name }} "). – Mikael H Apr 08 '20 at 12:22