ansible - Ansible:regex_search 过滤器比较以及如何调试 when 子句
问题描述
我今天花了一些时间尝试编写一些 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 | regex_search(item)
cat /tmp/cmdoutput
aa b c d xx
aa f g h yy
bb i j k xx
这将产生以下输出:
$ ansible-playbook test.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [test] **********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]
TASK [shell] *********************************************************************************************************************
changed: [localhost]
TASK [debug] *********************************************************************************************************************
ok: [localhost] => (item=None) => {
"filtered_output": "aa b c d xx"
}
ok: [localhost] => (item=None) => {
"filtered_output": ""
}
TASK [debug] *********************************************************************************************************************
skipping: [localhost] => (item=None)
skipping: [localhost] => (item=None)
TASK [debug] *********************************************************************************************************************
skipping: [localhost] => (item=None)
ok: [localhost] => (item=None) => {
"msg": "do action that inserts bb .* yy"
}
PLAY RECAP ***********************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0
即"filtered_output": ""
,但以下比较不匹配时。
所以我的问题是:
- 为什么条件匹配时不进行第二次调试
""
? - regex_search 的输出是什么类型的对象?- 一个字符串或一个数组或其他东西?
- 有没有,或者我在哪里可以找到有关
regex_search
过滤器的更多信息?- (此时 AFAICT)它不在jinja doco中,仅作为简短示例出现在官方文档的过滤器部分
我的 Ansible 版本:2.5.1
谢谢
解决方案
回答你的问题
为什么条件匹配“”时第二次调试不?
当没有“ NoneType ”类型的正则表达式匹配对象时返回。这种类型没有长度。而不是测试空字符串(题外话见E602)
when: cmdoutput.stdout|regex_search(item) == ""
使用(你的例子中已经有了它)。
when: not cmdoutput.stdout|regex_search(item)
推荐阅读
- reactjs - react-bootstrap-typeahead:无法使用回车键从下拉列表中选择突出显示的项目
- angular-material - 按钮组的角材料恒定宽度
- python-3.x - 如何将文本数据集(问题和答案)加载到 numpy 数组中以训练 keras 模型
- ansible-2.x - 在 Ansible 中,如何迭代具有数组的标准输出?
- windows - git-bash: SyntaxError: missing ) 在参数列表之后
- authorize.net - 在 Authorize.net CIM 中,为什么我的第一笔交易没有显示在客户历史记录中?
- asp.net - ASP.NET:启用逗号作为 cookie 分隔符而不使用不雅的 hack?
- airflow - 在 iMac 上安装 Apache-Airflow - 气流 initdb - ImportError:无法导入名称对象
- sql - 左连接实现减号不起作用
- javascript - 如何在javascript中取消来自window.onclick的点击