本文介绍了在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')}

我得到的错误是

为了获得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数组中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:15