blockinfile & lineinfile both are not working in RHEL 7.6

95 views
Skip to first unread message

Deepan M

unread,
Feb 26, 2019, 9:49:35 AM2/26/19
to Ansible Project

Hello Guys,

 Can someone please help me to resolve this issue.

below playbook i have created to remove dns entries from resolv.conf and its working perfect on rhel 6 but not in rhel 7.6, is there any bug or i need to add any parameters ?




Playbook :- blockinfile
---
- hosts: TC2
  become: yes
  tasks:
 - name: remove block regexps
    blockinfile:
      path: /etc/resolv.conf
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 "
      block: |
         nameserver 13.2.7.9  
         nameserver 13.2.7.10
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes

Playbook :- lineinfile
---
- hosts: TC2
  become: yes
  tasks:
  - name: copy the file
    shell: cp -p /etc/resolv.conf /tmp/resolv.conf.$(date +%F_%R)
  - name: remove block lines
    lineinfile:
      dest: /etc/resolv.conf
      regexp: "{{ item.line }}"
      state: absent
    with_items:
      - { line: 'nameserver 13.2.7.8' }
      - { line: 'nameserver 13.2.7.9' }
      - { line: 'nameserver 13.2.7.10' }
      - { line: 'search eu.ro.net mrtools.ro.com mit.gbr.ro.com' }



Client server:-

Hostname: testmachine
 Distro:   [redhat-release] Red Hat Enterprise Linux Server release 7.6 (Maipo)
           [os-release] Red Hat Enterprise Linux 7.6 (Maipo)
Booted kernel:  3.10.0-957.1.3.el7.x86_64

ansible control node:-
ansible 2.7.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/q5c9nf32/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Sep 12 2018, 05:31:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]



Thanks,
Deepan M

Deepan M

unread,
Feb 26, 2019, 10:36:54 AM2/26/19
to Ansible Project
Can someone please help me.

fusillator

unread,
Feb 26, 2019, 1:23:25 PM2/26/19
to Ansible Project
Are you sure the problem is ansible?
the playbook fails? what's the shown message?
Or the problem is that the configuration are reset on reboot?

regards

Luca

fusillator

unread,
Feb 26, 2019, 1:34:04 PM2/26/19
to Ansible Project
take a look at /var/log/messages for line with
updated /etc/resolv.conf
see more details at https://access.redhat.com/solutions/7412

fusillator

unread,
Feb 26, 2019, 2:27:49 PM2/26/19
to Ansible Project
Furthermore I don't get why you use the argument block with blockinfile in order to remove the block.. isn't unnecessary?

regards

Luca

Deepan M

unread,
Feb 27, 2019, 4:54:46 AM2/27/19
to Ansible Project
Hello Luca,


Are you sure the problem is ansible?
:-Not Sure, same playbook is working on rhel6 but not in rhel7, means no error.

the playbook fails? what's the shown message? 

:-no, playbook is working successfully in rhel 6, but rhel 7 its failing, not only resolv.conf even i tried with dummy file created on /tmp however that also failed.

:-Or the problem is that the configuration are reset on reboot?

no configuration issue

Furthermore I don't get why you use the argument block with blockinfile in order to remove the block.. isn't unnecessary?

:- is there any module did ansible is having it for removing block in file ?

Thanks,
Deepan M

Deepan M

unread,
Feb 27, 2019, 5:16:16 AM2/27/19
to Ansible Project
Hello Luca,

 One more information about this blockinfile, i facing problem with state: absent only not with state: present, means i can able to add entries, not able to remove entries.

Thanks,
Deepan M

fusillator

unread,
Feb 27, 2019, 5:27:36 AM2/27/19
to Ansible Project
blockinfile is the right choice
anyway I'm not sure if the argument block is necessary

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
nameserver 8.8.8.8
nameserver 13.2.7.8
nameserver 13.2.7.9
nameserver 13.2.7.10
search eu.ro.net mrtools.ro.com mit.gbr.ro.com

fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml
- hosts: localhost

  tasks:
  - name: remove block regexps
    blockinfile:
      path: "./resolv.conf"

      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8"
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes


fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [remove block regexps] ***************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0  

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
nameserver 8.8.8.8

Can you launch the playbook with increased verbosity to analyze the failure?
Maybe it could be useful the debug feature to shorten the test:


Unfortunatly at the moment I'm not on a rhel host.
Try also to check audit log for selinux restrictions
sealert -a /var/log/audit.log


regards

Luca

fusillator

unread,
Feb 27, 2019, 5:42:46 AM2/27/19
to Ansible Project
try to check if the marker line as blank/control/not printable characters at the end of the line
xxd resolv.conf

Deepan M

unread,
Feb 27, 2019, 5:59:05 AM2/27/19
to Ansible Project
Hi Luca,

 Perfectly working what you have suggested. 

I need one more suggestion, below is the content i want remove but when the line ends with #name its not working ? do you know how to remove with that also?

Playbook:-

 cat resolve.yml
---
- hosts: TC2
  become: yes
  tasks:
  - name: remove block regexps
    blockinfile:
      path: /tmp/resolv.conf
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8  # dns1"
      block: |
          nameserver 13.2.7.9   # dns2
          nameserver 13.2.7.10  # dns3
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes



Client Side:-
# cat resolv.conf
nameserver 13.2.7.8     # dns1
nameserver 13.2.7.9     # dns2
nameserver 13.2.7.10    # dns3

Thanks,
Deepan M

fusillator

unread,
Feb 27, 2019, 6:06:58 AM2/27/19
to Ansible Project
So you solved..
The blank counts for the matching of the marker lines..

fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [remove block regexps] ***************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0  

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
nameserver 8.8.8.8
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
resolv.conf                            resolv.conf.3339.2019-02-26@20:21:57~  resolv.conf.3740.2019-02-27@11:09:34~  resolv.conf.4381.2019-02-27@12:03:36~ 
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
resolv.conf                            resolv.conf.3339.2019-02-26@20:21:57~  resolv.conf.3740.2019-02-27@11:09:34~  resolv.conf.4381.2019-02-27@12:03:36~ 
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf.4381.2019-02-27@12\:03\:36~
nameserver 8.8.8.8
nameserver 13.2.7.8 #dns

nameserver 13.2.7.9
nameserver 13.2.7.10
search eu.ro.net mrtools.ro.com mit.gbr.ro.com
fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml
- hosts: localhost
  tasks:
  - name: remove block regexps
    blockinfile:
      path: "./resolv.conf"
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 #dns"

      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes

regards

Luca

Deepan M

unread,
Feb 27, 2019, 6:17:11 AM2/27/19
to Ansible Project
Hi Luca,

I tried by removing blank space, even though its not working!  seems # is not allowing to execute, can you please tell what can be done here!

 cat resolve.yml
---
- hosts: TC2
  become: yes
  tasks:
  - name: remove block regexps
    blockinfile:
      path: /tmp/resolv.conf
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 #dns1"
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes


Thanks,
Deepan M

Luca Cazzaniga

unread,
Feb 27, 2019, 6:25:22 AM2/27/19
to ansible...@googlegroups.com
I dont know for me it s working.. see my test in the prevoius answer. try to double check the yaml specification

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2dc1ca38-7c49-4342-8980-aeeddc986552%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Deepan M

unread,
Feb 27, 2019, 6:36:37 AM2/27/19
to Ansible Project
Hi Luca,

See below highlighted, seems on both playbook and file, you have removed blank space, thats why its working for you! but in my case blank is present, please see my output.

can you please my below info on your system and check what its showing.

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf.4381.2019-02-27@12\:03\:36~ 
nameserver 8.8.8.8
nameserver 13.2.7.8 #dns
nameserver 13.2.7.9
nameserver 13.2.7.10
search eu.ro.net mrtools.ro.com mit.gbr.ro.com
fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml 
- hosts: localhost
  tasks:
  - name: remove block regexps
    blockinfile:
      path: "./resolv.conf"
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 #dns"
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes

myoutput:-
 cat resolv.conf
nameserver 13.2.7.8     # dns1
nameserver 13.2.7.9     # dns2
nameserver 13.2.7.10    # dns3

 cat resolve.yml
---
- hosts: TC2
  become: yes
  tasks:
  - name: remove block regexps
    blockinfile:
      path: /tmp/resolv.conf
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8     # dns1"
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes



Thanks,
Deepan M

Luca Cazzaniga

unread,
Feb 27, 2019, 7:12:20 AM2/27/19
to ansible...@googlegroups.com
Interesting I'm commuting at the moment, I will test It tonight

fusillator

unread,
Feb 27, 2019, 10:48:48 AM2/27/19
to Ansible Project
Consider using the replace module to normalize the space incongruence before the blockinfile task..
Something like

- replace:
    path: resolv.conf
    regexp: '^(.*)\s+#\s+(.*)$'
    replace: '\1 # \2'
    backup: yes

unfortunately I can't try the playbook in this moment.

Regards

Luca


Deepan M

unread,
Feb 27, 2019, 11:26:41 AM2/27/19
to Ansible Project
Hello Luca,

  Replace can be removed single line, but how we can remove block ?

Thanks,
Deepan M

Deepan M

unread,
Feb 27, 2019, 11:59:45 AM2/27/19
to Ansible Project


Hello Luca,

Below playbook no result, seems because of #name its not working i guess.
---
- hosts: TC2
  become: yes
  tasks:
  - name: copy the file
    shell: cp -p /tmp/resolv.conf /tmp/resolv.conf.$(date +%F_%R)
  - name: remove block lines
    replace: dest=/tmp/resolv.conf regexp='{{ item.line }}' replace='{{ item.rep }}'
    with_items:
      - { line: '^nameserver 13.2.7.8     # dns1', rep: '' }
      - { line: '^nameserver 13.2.7.9     # dns2', rep: '' }
      - { line: 'nameserver 13.2.7.10    # dns3', rep: '' }
      - { line: 'search eu.ro.net mrtools.ro.com mit.gbr.ro.com', rep: ''}


:-Below playbook is working but not deleting the entire line, example the nameserver 13.2.7.8     # dns1, "nameserver 13.2.7.8" is getting deleted but # dns1 still persist in file!  
---
- hosts: TC2
  become: yes
  tasks:
  - name: copy the file
    shell: cp -p /tmp/resolv.conf /tmp/resolv.conf.$(date +%F_%R)
  - name: remove block lines
    replace: dest=/tmp/resolv.conf regexp='{{ item.line }}' replace='{{ item.rep }}'
    with_items:
      - { line: '^nameserver 13.2.7.8', rep: '' }
      - { line: '^nameserver 13.2.7.9', rep: '' }
      - { line: 'nameserver 13.2.7.10', rep: '' }
      - { line: 'search eu.ro.net mrtools.ro.com mit.gbr.ro.com', rep: ''}


Thanks,
Deepan M

fusillator

unread,
Feb 27, 2019, 12:58:09 PM2/27/19
to Ansible Project
Replace should substitute all instance matching the regexp, as for the manual

This module will replace all instances of a pattern within a file.
Try this

- hosts: localhost
  tasks:
  - name: sort out blank discrepancy
    replace:
   path: resolv.conf
   regexp: '^(.*)\s+#\s+(.*)$'
   replace: '\1 # \2'
   backup: yes
  - name: remove block regexps

    blockinfile:
      path: "./resolv.conf"
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 #dns"
      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
      backup: yes

Anyway I didn't test it yet.
Regards

Luca

fusillator

unread,
Feb 27, 2019, 7:48:09 PM2/27/19
to Ansible Project
I'm finally home
Sorry the previous regexp was completely incorrect. Now I fixed it with a couple of tests..
And here it is

fusillator@catorcio:~/Code/ansible/test$ cat blockinfile.yml
- hosts: localhost
  tasks:
  - name: sort out blank discrepancy
    replace:
      path: resolv.conf
      regexp: '^nameserver\s+([^ ]+)\s+#(.*)'
      replace: 'nameserver \1 #\2'

      backup: yes
  - name: remove block regexps
    blockinfile:
      path: "./resolv.conf"
      marker: "{mark}"
      marker_begin: "nameserver 13.2.7.8 # dns1"

      marker_end: "search eu.ro.net mrtools.ro.com mit.gbr.ro.com"
      state: absent
fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
nameserver 8.8.8.8
nameserver 13.2.7.8     # dns1
nameserver 13.2.7.9     # dns2
nameserver 13.2.7.10    # dns3
search eu.ro.net mrtools.ro.com mit.gbr.ro.com
fusillator@catorcio:~/Code/ansible/test$ ansible-playbook blockinfile.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [sort out blank discrepancy] *********************************************************************************************************************************************************************************************************************************************
changed: [localhost]


TASK [remove block regexps] ***************************************************************************************************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0  

fusillator@catorcio:~/Code/ansible/test$ cat resolv.conf
nameserver 8.8.8.8

Regards

Luca

Reply all
Reply to author
Forward
0 new messages