0

Need to register results from loop (task #1) and check when command.results stderr is not empty(task #2) relatively to loop items, so I can use this condition at new tasks.

The problem is that:

when condition from task #2 doesn't work cause I can't understand how to divide command.results relatively to loop items.

Is there solution to work around?

---
- 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 of command.results | map(attribute='stderr') | list:

ok: [projectname-OPENVZHOST-S1] => {
    "msg": [
        [], 
        []
    ]
}

Please get advice or solution as you see. Or what I need to research.

Regards

PS: have an idea to create folder and then check if it exist or not exist, but have no idea how to relation dict to results

0

You want to use the length filter.

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

Or even better, just access stderr directly.

- command.stderr != ""

Full playbook I used is here:

---

- name: test playbook
  hosts: all
  vars:
    my_list_var: []
  tasks:
    - name: debug
      debug:
        msg: "list is empty"
      when:
        - my_list_var | list | length == 0
    - name: ls command
      command: "ls -l /tmp"
      register: result
    - name: debug 2
      debug:
        msg: "no errors"
      when: result.stderr == ""
4
  • thanks, but it doesn't work: at first option command.results in any way has length biger 0 cause consist off two empty fields, at second option get error: 'dict object' has no attribute 'stderr' – Kein Mar 19 '19 at 20:59
  • Interesting. I tested these and didn't have those issues. What version of Ansible are you using? – MacGruber Mar 22 '19 at 14:52
  • Thanks, i'm using 2.6.5. version. Is there possibility to see your testing playbook? – Kein Mar 22 '19 at 18:02
  • Sorry I was on vacation and couldn't access my work computer which contained the playbook. I'm running Ansible 2.7.0. I updated my answer with the full playbook I used for testing. You can change the "==" to "!=" for your case. – MacGruber Apr 15 '19 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.