Ansible: Deleting matching IPs from hosts file using regex

+1 vote

I'm just trying to change the hostname of the remote node using:

- name: Change the hostname
  lineinfile: dest=/etc/hosts
              regexp='.*{{ item }}$'
              line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
              state=present
  when: hostvars[item].ansible_default_ipv4.address is defined
  with_items: "{{ groups['dbservers'] }}"

All this does is add IP hostname FQDN at the end of /etc/hosts file. But I'm want it to remove the existing entry and add new one. This is what I tried so far:

    - name: Change the hostname
      lineinfile: dest=/etc/hosts
#                  regexp='.*{{ item }}$'
                  regexp='{{ hostvars[item].ansible_default_ipv4.address }}'
                  state=absent
#                  line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
#                  state=present
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

But this error comes if I run this:

The offending line appears to be:
#                  regexp='.*{{ item }}$'
                  regexp="{{ hostvars[item].ansible_default_ipv4.address }}"
                  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.

Am I doing the right thing or there some other way to achieve this?

P.S. I've tried adding "to" but that didn't help either.

Jul 25, 2018 in Ansible by Atul
• 5,510 points
144 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Ansible notation isn't the same as YAML where you can use equal signs. Your problem isn't quoting but you inserted comments at the wrong place. Try this syntax which is valid:

- name: Change the hostname
  lineinfile:
    dest: /etc/hosts
    # regexp: '.*{{ item }}$'
    regexp: '{{ hostvars[item].ansible_default_ipv4.address }}'
    state: absent
    # line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
    # state: present
  when: hostvars[item].ansible_default_ipv4.address is defined
  with_items: "{{ groups['dbservers'] }}"
answered Jul 25, 2018 by DareDev
• 6,710 points

Related Questions In Ansible

0 votes
1 answer
+1 vote
1 answer

Is it possible to append a large amount of text to a file using Ansible?

Here is concept which you can follow. blockinfile is ...READ MORE

answered Jul 30, 2018 in Ansible by Atul
• 10,220 points
1,653 views
0 votes
1 answer

Getting numbered build artefact using ansible from jenkins

You can get the build number with ...READ MORE

answered Aug 20, 2018 in Ansible by ajs3033
• 7,120 points
481 views
0 votes
1 answer

How to encrypt and existing file using Ansible Vault?

Hey @Kyraa, to encrypt an existing file ...READ MORE

answered Jan 29 in Ansible by Kavya
179 views
+13 votes
2 answers
0 votes
1 answer
0 votes
1 answer

stopping different hosts from reading the same value when usign ansible in loop

Try using this. Create a new directory ...READ MORE

answered Jul 27, 2018 in Ansible by DareDev
• 6,710 points
26 views
0 votes
1 answer

How can I create a new user using ansible ploybook?

You're switching from the root user. Root ...READ MORE

answered Mar 29, 2018 in Ansible by DareDev
• 6,710 points
81 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.