Finding a string within an stdout_lines array

73 views
Skip to first unread message

jamesv...@gmail.com

unread,
Jun 14, 2016, 2:02:51 PM6/14/16
to Ansible Project
I am trying to find whether a certain letter exists in an stdout_lines array. 

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

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

The error I am getting is 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'"}

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


jamesv...@gmail.com

unread,
Jun 14, 2016, 3:31:43 PM6/14/16
to Ansible Project

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'}
Reply all
Reply to author
Forward
0 new messages