Ansible recursive checks in playbooks

二次信任 提交于 2019-12-12 03:37:32

问题


We need to go through this structure

Zone spec https://gist.github.com/git001/9230f041aaa34d22ec82eb17d444550c

I was able to run the following snipplet but now I'm stucked at the error checking.

playbook

--
- hosts: all
  gather_facts: no

  vars_files:
    - "../doc/application-zone-spec.yml"

  roles:
    - { role: ingress_add, customers: "{{ application_zone_spec }}" }

role

- name: check if router exists
  shell: "oc get dc -n default {{ customers.zone_name }}-{{ item.type }}"
  with_items: "{{ customers.ingress }}"
  ignore_errors: True
  register: check_router

- name: Print ingress hostnames
  debug: var=check_router

- name: create new router
  shell: "echo 'I will create a router'"
  with_items: "{{ customers.ingress }}"
  when: check_router.rc == 1

Output of a ansible run

https://gist.github.com/git001/dab97d7d12a53edfcf2a69647ad543b7

The problem is that I need to go through the ingress items and I need to map the error of the differnt types from the "check_router" register.

It would be nice to make something like.

Pseudo code.

Iterate through the "customers.ingress"
  check in "check_router" if the rc is ! 0
    execute command.

We use.

ansible-playbook --version
ansible-playbook 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

回答1:


You can replace the second loop with:

- name: create new router
  shell: "echo 'I will create a router with type {{ item.item }}'"
  with_items: "{{ check_router.results }}"
  when: item.rc == 1

This will iterate over every step of check_route loop and you can access original items via item.item.



来源:https://stackoverflow.com/questions/39254122/ansible-recursive-checks-in-playbooks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!