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,530 points
225 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,810 points

Related Questions In Ansible

0 votes
1 answer
+1 vote
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,240 points
2,250 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,280 points
592 views
+13 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 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
124 views
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,810 points
32 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,810 points
99 views