adding escape characters in replace module

8 views
Skip to first unread message

anil kumar

unread,
Feb 27, 2020, 10:52:42 AM2/27/20
to Ansible Development
Hi All,

I have file below, would like to comment out the line with hmac-sha1 and uncomment like with #Commened for LINUXMSB 2013'

$ cat sshd_config
MACs hmac-sha...@openssh.com,hmac-sha...@openssh.com,hmac-sha2-512,hmac-sha2-256
#Added below for LINUXMSB 2013
MACs    hmac-sha2-512,hmac-sha2-256,hmac-sha1

Interestingly if dont use loop(with_items), it works fine. When i use with with_item, it adds another escape character, Am i missing something?

Here is the code:

$ cat sshd.yml
---
- hosts: localhost
  connection: local
  tasks:
  #- replace:
  #   path: sshd_config
  #   regexp: '(.*hmac-sha1.*)'
  #   replace: '#\1'
  #- replace:
  #   path: sshd_config
  #   regexp: '#Commented for LINUXMSB 2013 '
  - replace:
     path: sshd_config
     regexp: "{{item.exp}}"
     replace: "{{item.rep}}"
    with_items:
    - { exp: '(.hmac-sha1.)', rep: '# \1' }
    - { exp: '#Commented for LINUXMSB 2013 ', rep: '' }

Here is ansible run:

$ ansible-playbook  sshd.yml
 [WARNING]: Could not match supplied host pattern, ignoring: all
 [WARNING]: provided hosts list is empty, only localhost is available

PLAY [localhost] ****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]
TASK [replace] ******************************************************************************************************************************************************
ok: [localhost] => (item={u'rep': u'# \\1', u'exp': u'(.hmac-sha1.)'})
ok: [localhost] => (item={u'rep': u'', u'exp': u'#Commented for LINUXMSB 2013 '})
PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
Reply all
Reply to author
Forward
0 new messages