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

查看:365
本文介绍了在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

myrole.yml

---
- hosts: windows
  gather_facts: false
  roles:
    - all_servers
    - {role: production_server, when: prod_fact.find('P')}

我得到的错误是

致命:[主机名]:失败! => {失败":true,"msg":错误!条件检查'{{prod_fact}}.find('P')'失败.错误是:ERROR!模板字符串模板错误:预期令牌' ,',得到了'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天全站免登陆