今天我花了一些时间尝试编写一些 Ansible 脚本,以便仅在相关命令输出中不存在相应行的情况下运行命令。经过一番尝试和错误后,我得到了一些对我有用的东西,但我不清楚为什么我与空字符串的初始比较不起作用。
这是一个演示我的问题的剧本:
- name: test
hosts: localhost
tasks:
- shell: "cat /tmp/cmdoutput"
register: cmdoutput
- debug: var=filtered_output
vars:
filtered_output: "{{ cmdoutput.stdout | regex_search(item) }}"
with_items:
- "aa .* xx"
- "bb .* yy"
- debug: msg="do action that inserts {{ item }}"
with_items:
- "aa .* xx"
- "bb .* yy"
when: cmdoutput.stdout | regex_search(item) == ""
- debug: msg="do action that inserts {{ item }}"
with_items:
- "aa .* xx"
- "bb .* yy"
when: not cmdoutput.stdout …
Run Code Online (Sandbox Code Playgroud) ansible ×1