G
G
gremlintv22019-03-19 11:52:38
Ansible
gremlintv2, 2019-03-19 11:52:38

ANSIBLE How to correlate dictionary elements and task results in a loop?

(Code and debug under spoiler)
It is necessary to register the results from the loop ( task № 1) and check if ( ) is not command.resultsstderrempty task № 2relative to the elements of the loop loop: "{{ lookup('dict', openvz_instances) }}"so that I can use this condition in new tasks.
The problem is that:
whenthe condition from task № 2doesn't work because I can't figure out how to split command.results with respect to dictionary elements.
Is there a solution to get around this issue?

PLAYBOOK
---
- name: "CHECK IF EXIST OPENVZ CONTAINER"
  hosts: "projectname-OPENVZHOSTS"
  gather_facts: yes
  remote_user: root
  vars:
    openvz_check:              "yes"
    openvz_instances:
      opwnvz_instance_1:  
        vmid:                  "119"
      opwnvz_instance_2:  
        vmid:                  "118"

  tasks:

  - name: "Ping my hosts"
    ping:


  - name: "1. Register exist containers"
    shell: echo $(vzlist -a {{ item.value.vmid }})
    register: command
    when: openvz_check == "yes"
    loop: "{{ lookup('dict', openvz_instances) }}"
    ignore_errors: yes

  - name: "2. Create variable from command"
    debug: msg="{{ command.results | map(attribute='stderr_lines') | list  }}"
    when: 
      - openvz_check == "yes" 
      - command.results | map(attribute='stderr') | list != "" 
    loop: "{{ lookup('dict', openvz_instances) }}"
    ignore_errors: yes

  - debug: msg="{{ command.results | map(attribute='stderr_lines') |list  }}"

Debug command.results | map(attribute='stderr') | list:
DEBUG
ok: [projectname-OPENVZHOST-S1] => {
    "msg": [
        [], 
        []
    ]
}

Please help with advice or a solution as you see it.
PS: I have an idea to create a folder and then check if it exists or not, but I don't know how to correlate its existence with items from the dictionary.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Logout_90, 2019-04-25
@gremlintv2

- command.results | map(attribute='stderr') | list != ""

This condition seems to be not quite correctly indicated.
Since the sheet, then you need to check
- command.results | map(attribute='stderr') | list != []

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question