The Ansible playbook code below works, but it relies on a shell out to egrep. I would like to know the "right way" to do this (using Ansible modules, Jinja2 filters, etc., but no external binaries) for my own personal growth.
I tried several things, but couldn't find a "native" solution that worked. (The less said about my attempts to use the ini filter, the better!)
- name: "Check for {{exclude_file}}"
stat:
path: "{{playbook_dir}}/{{exclude_file}}"
follow: yes
register: exclude_file_stat
delegate_to: localhost
- name: "Check if {{username}} is in {{exclude_file}} (if exists)"
shell: "egrep '^[ \t]*{{username}}[ \t]*$' {{playbook_dir}}/{{exclude_file}} || true"
changed_when: false
register: exclude_file_egrep
delegate_to: localhost
when: exclude_file_stat.stat.exists == true
- name: "Fail if {{exclude_file}} exists and {{username}} found"
fail:
msg: "{{username}} found in {{exclude_file}}"
when: exclude_file_stat.stat.exists == true and exclude_file_egrep.stdout != ""