在 stdout_lines 数组中查找字符串
Finding a string within an stdout_lines array
我试图找出某个字母是否存在于 stdout_lines 数组中。
如果在 stdout_output 中找到 'P',我希望角色为 运行。
stdout_lines 数组如下所示:
"stdout": "P\r\nA\r\nS\r\nI\r\n", "stdout_lines": ["P", "A", "S", "I"]
myrole.yml
---
- hosts: windows
gather_facts: false
roles:
- all_servers
- {role: production_server, when: prod_fact.find('P')}
我收到的错误是
fatal: [hostname]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check '{{prod_fact}}.find('P')' failed. The error was: ERROR! template error while templating string: expected token ',', got 'string'"}
为了获得 stdout_variable 我正在使用 set_fact
---
- name: Check Env Type and Save it in Var=prod_fact
script: files/CheckEnvType.ps1 -hostname {{inventory_hostname}}
register: result
- set_fact:
prod_fact: "{{result.stdout_lines | default('')}}"
错误信息很奇怪,我无法在Ansible 2中重现。但是你的条件仍然不成立,列表没有find方法。在 Ansible 中,您可以使用 in
:
搜索列表
roles:
- all_servers
- {role: production_server, when: '"P" in prod_fact'}
我试图找出某个字母是否存在于 stdout_lines 数组中。
如果在 stdout_output 中找到 'P',我希望角色为 运行。
stdout_lines 数组如下所示:
"stdout": "P\r\nA\r\nS\r\nI\r\n", "stdout_lines": ["P", "A", "S", "I"]
myrole.yml
---
- hosts: windows
gather_facts: false
roles:
- all_servers
- {role: production_server, when: prod_fact.find('P')}
我收到的错误是
fatal: [hostname]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check '{{prod_fact}}.find('P')' failed. The error was: ERROR! template error while templating string: expected token ',', got 'string'"}
为了获得 stdout_variable 我正在使用 set_fact
---
- name: Check Env Type and Save it in Var=prod_fact
script: files/CheckEnvType.ps1 -hostname {{inventory_hostname}}
register: result
- set_fact:
prod_fact: "{{result.stdout_lines | default('')}}"
错误信息很奇怪,我无法在Ansible 2中重现。但是你的条件仍然不成立,列表没有find方法。在 Ansible 中,您可以使用 in
:
roles:
- all_servers
- {role: production_server, when: '"P" in prod_fact'}