在 stdout_lines 数组中查找字符串 [英] Finding a string within an stdout_lines array

查看:27
本文介绍了在 stdout_lines 数组中查找字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找 stdout_lines 数组中是否存在某个字母.

I am trying to find whether a certain letter exists in an stdout_lines array.

如果在 stdout_output 中找到P",我希望角色运行.

I want the role to run if there is a 'P' found in the stdout_output.

stdout_lines 数组如下所示:

The stdout_lines array looks like this:

"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')}

我得到的错误是

致命:[主机名]:失败!=> {"failed": true, "msg": "错误!条件检查 '{{prod_fact}}.find('P')' 失败.错误是:错误!模板字符串时模板错误:预期令牌',', 得到 'string'"}

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

In order to get the stdout_variable I am using 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 搜索列表:

The error message is strange and I can not reproduce it in Ansible 2. But your condition still will not work, a list does not have a find method. In Ansible you can search a list with in:

roles:
  - all_servers
  - {role: production_server, when: '"P" in prod_fact'}

这篇关于在 stdout_lines 数组中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆