Ansible嵌套循环依赖

时间:2017-06-28 22:00:26

标签: ansible

我正在尝试向服务API提交请求,然后在进行其他调用之前查询状态。

在这个例子中,我正在尝试为(CallOne,CallTwo)制作API,我应该寻找哪种类型的循环?我已经尝试过嵌套的forloop,但是我无法使循环依赖工作,我不确定ansible是否支持这样的循环。

- name: API - callOne
  uri:
    url: "https://localhost/api/commands/callOne"
    method: POST
    user: "admin"
    password: "admin"
  register: callOne
  delegate_to: localhost

- name: API - callOne - awating async task to complete
  uri:
    url: "https://localhost/api/commands/{{callOne.json['id']}}"
    method: GET
    user: "admin"
    password: "admin"
  register: callOne_repsonse
  until: callOne_repsonse.json.active == false and callOne_repsonse.json.success == true
  retries: 10
  delay: 15
  delegate_to: localhost

- debug: msg={{callOne_repsonse.json.resultMessage}}



- name: API - callTwo
  uri:
    url: "https://localhost/api/commands/callTwo"
    method: POST
    user: "admin"
    password: "admin"
  register: callTwo
  delegate_to: localhost

- name: API - callTwo - awating async task to complete
  uri:
    url: "https://localhost/api/commands/{{callTwo.json['id']}}"
    method: GET
    user: "admin"
    password: "admin"
  register: callTwo_repsonse
  until: callTwo_repsonse.json.active == false and callTwo_repsonse.json.success == true
  retries: 10
  delay: 15
  delegate_to: localhost

- debug: msg={{callTwo_repsonse.json.resultMessage}}

1 个答案:

答案 0 :(得分:1)

根据我对你的问题的理解(当你谈到依赖关系时不确定是这种情况,但在你的问题中没有一个是显而易见的),我想解决方案会是这样的:

- name: API - call
  uri:
    url: "https://localhost/api/commands/{{ item }}"
    method: POST
    user: "admin"
    password: "admin"
  register: call
  delegate_to: localhost
  with_items:
    - callOne
    - callTwo

- name: API - awaiting async task to complete
  uri:
    url: "https://localhost/api/commands/{{ item.json.id }}"
    method: GET
    user: "admin"
    password: "admin"
    register: call_response
  until: call_response.json.active == false and call_response.json.success == true
  retries: 10
  delay: 15
  delegate_to: localhost
  with_items: "{{ call.results }}"

- debug: msg={{ item.json.resultMessage }}
  with_items: "{{ call_response.results }}"