下面的Ansible剧本代码可以工作,但它依赖于egrep的外壳。我想知道做这件事的“正确方法”(使用Ansible模块、Jinja2过滤器等,但没有外部二进制文件),以促进我自己的个人成长。
我尝试了几件事,但找不到一个有效的“原生”解决方案。(我尝试使用ini过滤器的次数越少,越好!)
- 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 != ""发布于 2019-04-05 22:31:29
下面这出戏做你要找的东西吗?
- name: "Fail if {{ exclude_file }} exists and {{ username }} found"
fail:
msg: "{{ username }} found in {{ exclude_file }}"
when:
- exclude_file is exists
- lookup('file', exclude_file) is search(username)https://stackoverflow.com/questions/55532117
复制相似问题