gpt4 book ai didi

Ansible:regex_search 过滤器比较以及如何调试 when 子句

转载 作者:行者123 更新时间:2023-12-05 03:02:01 27 4
gpt4 key购买 nike

我今天花了一些时间尝试编写一些 Ansible play,以便仅在相关命令输出中不存在相应行时才运行命令。经过一些试验和错误后,我得到了一些对我有用的东西,但我不清楚为什么我最初与空字符串的比较不起作用。

这是一个演示我的问题的剧本:

- 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 中并且只出现在 filters section of the official docs 中作为简短的例子

我的 Ansible 版本:2.5.1

谢谢

最佳答案

问:当条件匹配“”时,为什么第二次调试不?

A:当没有NoneType 类型的正则表达式匹配对象时返回。 此类型没有长度。而不是测试空字符串(题外话见 Ansible Lint: Don’t compare to empty string )

    when: cmdoutput.stdout|regex_search(item) == ""

使用(你的例子中已经有了)

    when: not cmdoutput.stdout|regex_search(item)

None 和空字符串的计算结果为 False,非空字符串的计算结果为 True


<支持>

问:“如何轻松地看到这个 NoneType 对象?”

A:使用过滤器 type_debug .比如搜索成功

    - debug:
msg: "{{ cmdoutput.stdout|regex_search('a') }}"
vars:
cmdoutput:
stdout: abc

结果是一个字符串

  msg: a

可以看到结果的类型

    - debug:
msg: "{{ cmdoutput.stdout|regex_search('a')|type_debug }}"
vars:
cmdoutput:
stdout: abc

给予

  msg: str

如果搜索失败,结果为

    - debug:
msg: "{{ cmdoutput.stdout|regex_search('x') }}"
vars:
cmdoutput:
stdout: abc
  msg: ''
    - debug:
msg: "{{ cmdoutput.stdout|regex_search('x')|type_debug }}"
vars:
cmdoutput:
stdout: abc
  msg: NoneType

关于Ansible:regex_search 过滤器比较以及如何调试 when 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55253746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com