Hi,
Perhaps you should better use slurp module to register the
content of the file and do some regexp to print what you want on
it...
https://docs.ansible.com/ansible/latest/modules/slurp_module.html#slurp-module
Regards,
I wish to search for all entries of string starting with "SSLFile" or starting with "<whitespache>SSLFile" in a file(httpd.conf) and register it to a variable and print all the matches found.--
The string is found as evident from the output and the file is not modified which is good; but I'm unable to print (debug) it. I get error as I try to print. Below is my playbook:
- name: Find entries
lineinfile:
path: "/tmp/httpd.conf"
regexp: "\\sSSLFile.*"
state: absent
check_mode: yes
changed_when: false
register: filedet
- debug:msg: "{{ filedet }}"
- debug:
msg: "{{ item.split()[1] }}"
with_items:
- "{{ filedet.stdout_lines }}"
I get the below error when i run the playbook:
ok: [10.9.9.11] => {
"backup": "",
"changed": false,
"diff": [
{
"after": "",
"after_header": "/tmp/httpd.conf (content)",
"before": "",
"before_header": "/tmp/httpd.conf (content)"
},
{
"after_header": "/tmp/httpd.conf (file attributes)",
"before_header": "/tmp/httpd.conf (file attributes)"
}
],
"found": 1,
"invocation": {
"module_args": {
"attributes": null,
"backrefs": false,
"backup": false,
"content": null,
"create": false,
"delimiter": null,
"directory_mode": null,
"firstmatch": false,
"follow": false,
"force": null,
"group": null,
"insertafter": null,
"insertbefore": null,
"line": null,
"mode": null,
"owner": null,
"path": "/tmp/httpd.conf",
"regexp": "\\sSSLFile.*",
"remote_src": null,
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"state": "absent",
"unsafe_writes": null,
"validate": null
}
},
"msg": "1 line(s) removed"
} TASK [debug] *******************************************************************
task path: /app/test.yml:924
ok: [10.9.9.11] => {
"msg": {
"backup": "",
"changed": false,
"diff": [
{
"after": "",
"after_header": "/tmp/httpd.conf (content)",
"before": "",
"before_header": "/tmp/httpd.conf (content)"
},
{
"after_header": "/tmp/httpd.conf (file attributes)",
"before_header": "/tmp/httpd.conf (file attributes)"
}
],
"failed": false,
"found": 1,
"msg": "1 line(s) removed"
}
} TASK [debug] *******************************************************************
task path: /app/test.yml:928
fatal: [10.9.9.11]: FAILED! => {
"msg": "'dict object' has no attribute 'stdout_lines'"
}Can you please suggest what is the correct way to print all the searched matched strings without modifying the file ? I wish to use the the registered variable to perform other actions later in the playbook.
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/47deda2a-1a8c-4387-9a5b-d2ea548ad752%40googlegroups.com.
- name: Slurp certificate entries
slurp:
src: "{{ httpd_home }}/conf/httpd.conf"
register: filecontent
- name: Find certificate entries
set_fact:
input: "{{ filecontent['content'] | b64decode }}"
- debug:
msg: "{{ input }}"
- name: Regex String
set_fact:
target: "{{ input | regex_replace('\\sSSLFile.*, '\\1') }}"
TASK [Regex String] ***************************************
task path: /app/test.yml:908
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 144, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 576, in _execute
self._task.post_validate(templar=templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 268, in post_validate
super(Task, self).post_validate(templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 384, in post_validate
value = templar.template(getattr(self, name))
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 584, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 539, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 773, in do_template
data = _escape_backslashes(data, myenv)
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 145, in _escape_backslashes
for token in jinja_env.lex(d2):
File "/usr/lib/python2.7/site-packages/jinja2/lexer.py", line 733, in tokeniter
name, filename)
TemplateSyntaxError: unexpected char u'\\' at 51
line 1 fatal: [10.9.9.11]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.