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 ffdfd
• 5,550 points
2,301 views

1 answer to this question.

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,890 points

Related Questions In Ansible

0 votes
1 answer

How do I wget a file from web server using shell in the ansible playbook

Hey Ayaan, you could probably use this ...READ MORE

answered Jan 24, 2019 in Ansible by Anushri
4,128 views
+1 vote
1 answer
0 votes
1 answer

How to remove duplicate entries from a file using Ansible?

Hi@akhtar, You can use the shell module to do your ...READ MORE

answered Sep 17, 2020 in Ansible by MD
• 95,460 points
4,880 views
+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,240 points
14,619 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,987 views
+2 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,890 points
687 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,890 points
884 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP