rahuL
rahuL

Reputation: 3420

Ansible regex to delete matching IPs from /etc/hosts

I'm attempting to change the hostname for the remote node and this section works:

- 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'] }}"

So that this does is, it appends the IP hostname FQDN to the end of the /etc/hosts file.

What I'm trying to achieve is to remove an existing entry and then add this section, and here's what I've tried:

    - 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'] }}"

However, this keeps showing the following error:

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.

Changing the quotes from '' to "" doesn't seem to work. My questions are:

Upvotes: 1

Views: 2603

Answers (2)

Srinu Mareti
Srinu Mareti

Reputation: 27

Here you go..
---
- name: host trick
  hosts: dev
  gather_facts: yes
  become: true
  pre_tasks:
    - name: Include fixed env variables
      include_vars: "group_vars/dev.yml"


  tasks:

  - debug: var=hostvars[groups['app'][0]].ansible_host
  
  - name: Update the /etc/hosts file with node name
    vars: 
         remove_host: "hostname.domain.com"
    tags: etchostsupdate
    become: yes
    become_user: root
    lineinfile:
      dest: /etc/hosts
      regexp: "{{ remove_host }}"
      line: "{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{item}}"
      state: absent
      backup: yes
    register: etchostsupdate
    when: hostvars[item]['ansible_facts']['default_ipv4'] is defined
    with_items:
      - "{{ groups['dev'] }}"



#ansible-playbook -i dev/hosts dev/remove_etc_hosts.yml -e "remove_host=hostname.domain.com"

Upvotes: 0

techraf
techraf

Reputation: 68439

You can't use Ansible notation (with equal signs) and treat it like YAML.

The problem with your code is not quoting, but the fact that you inserted comments where you shouldn't.

The following syntax is valid, yours is not:

- 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'] }}"

Upvotes: 1

Related Questions