ansible regex with lineinfile module not updating all host

19 views
Skip to first unread message

rrak...@gmail.com

unread,
Apr 4, 2018, 3:55:08 AM4/4/18
to Ansible Project

Esteemed Collegues, I have created a Playbook to replcae the Entry in a file using a lineinfile module, while i tried running it run and replace in entry on the one host while not on the another host, i don't understand why? as it doesn't report any error even on the debug mode. Do i need to have some another regex pattern.

---
- name: Playbook to replace the line in file
  hosts: all
  remote_user: root
  gather_facts: False
  serial: 4
  tasks:
    - name: replace line
      lineinfile:
        dest: /tmp/test_master
        state: present
        regexp: '^passwd: files ldap' #  line to be replaced
        line: 'passwd_compat: ldap'   #  new line which is replace existing , the one above
        backrefs: yes
        backup: yes
      register: nss

    - name: replace line
      debug: var=nss


My Hostvar Inventory File:

# test_regex_hosts
[hosts]
Dev-mauj
Dev-pranjala
[hosts:vars]
ansible_ssh_user=root
ansible_ssh_private_key_file=/Dev/ansibleSpotin

Destination File contents:

$ cat /tmp/test_master
#
# nsswitch.conf
#
passwd: files ldap
shadow:     files ldap
group:      files ldap
hosts:      files dns
bootparams: files
ethers:     files
netmasks:   files ldap
networks:   files ldap
protocols:  files ldap
rpc:        files ldap
services:   files ldap
netgroup:   files ldap
publickey:  files ldap
automount:  files ldap
printers:   user files ldap
sudoers:    ldap
aliases:    files ldap

Note: ansible 2.4

John Harmon

unread,
Apr 4, 2018, 4:11:14 PM4/4/18
to Ansible Project
You need to account for white space. Try:
regexp: '^passwd:.*files ldap'


rrak...@gmail.com

unread,
Apr 4, 2018, 11:49:12 PM4/4/18
to Ansible Project
Ah! that works John! thnx a mile.
Reply all
Reply to author
Forward
0 new messages