Reputation: 151
Please help me with your guidance here.
I want to comment a line in one config file and need to uncomment the other lline using ansible.
My file content as below.
`<include location="conf/basicUserRegistry.xml"/>`
this i want to comment
`<!--include location="conf/ldapUserRegistry.xml"/-->`
this line i want to uncomment
I tried with the below play book and its not showing any error even though its not changing any changes to the file.
- name: enable LDAP
hosts: localhost
tasks:
- name: disbale basic user-registry
lineinfile:
path: /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml
regexp: '^\s*<include location="conf/basicUserRegistry.xml">.*$'
line: '<!--include location="conf/basicUserRegistry.xml"-->;'
backrefs: yes
become: true
- name: enable LDAP USER_REGISTRY
hosts: localhost
tasks:
lineinfile:
dest: /opt/IBM/605CLM/JazzTeamServer/server/liberty/servers/clm/server.xml
regexp: '^(.*)<!--include location="conf/ldapUserRegistry.xml"-->(.*)$'
line: '<include location="conf/ldapUserRegistry.xml">;'
backrefs: yes
Output:
PLAY [enable LDAP] ******************************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************** ok: [localhost]
TASK [disbale basic user-registry] ************************************************************************************************************************** ok: [localhost]
PLAY RECAP ************************************************************************************************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0
Upvotes: 1
Views: 4018
Reputation: 2308
An arguably cleaner way to solve this would make use of the Ansible xml
module.
Example (assuming your example showed the complete file):
tasks:
- xml:
file: server.xml
xpath: /include
attribute: location
value: conf/ldapUserRegistry.xml
Upvotes: 2
Reputation: 151
Below is my playbook, Its not changing anything on the file
- name: LDAP Settings
hosts: myserver
vars:
mark_1: 'include location="conf/basicUserRegistry.xml"/'
mark_2: 'include location="conf/ldapUserRegistry.xml"/'
tasks:
- replace:
path: /opt/IBM/server/liberty/servers/clm/server.xml
regexp: "^<!--{{ mark_2 }}-->"
replace: "<{{ mark_2 }}>"
- replace:
path: /opt/IBM/server/liberty/servers/clm/server.xml
regexp: "^<{{ mark_1 }}>"
replace: "<!--{{ mark_1 }}-->"
backup: yes'
Upvotes: 0
Reputation: 68034
Is this the code that you're looking for?
vars:
mark_1: 'include location="conf/basicUserRegistry.xml"/'
mark_2: 'include location="conf/ldapUserRegistry.xml"/'
tasks:
- lineinfile:
path: server.xml
regex: "^<!--{{ mark_2 }}-->"
line: "<{{ mark_2 }}>"
- lineinfile:
path: server.xml
regex: "^<{{ mark_1 }}>"
line: "<!--{{ mark_1 }}-->"
It's not idempotent! If the regex exists the line will replace the line found. If the regex doesn't exist the *line" will be added anyway. To make it idempotent use replace module (example below).
Or, prehaps, this one ?
- lineinfile:
path: server.xml
regex: ".{{ mark_2 }}."
line: "<{{ mark_2 }}>"
- lineinfile:
path: server.xml
regex: ".{{ mark_1 }}."
line: "<!--{{ mark_1 }}-->"
or,
- replace:
path: server.xml
regexp: "^<!--{{ mark_2 }}-->"
replace: "<{{ mark_2 }}>"
- replace:
path: server.xml
regexp: "^<{{ mark_1 }}>"
replace: "<!--{{ mark_1 }}-->"
Upvotes: 0