uri模块中的多个URL

时间:2019-11-18 01:03:48

标签: ansible uri

考虑此验证码

- name: Retrieve the wf execution information
  hosts: all
  tasks:
    - name: Get execution info
      uri: 
        url: https://myurl
        user: my_user
        password: my_pass
        method: GET
        return_content: yes
      register: wf_runs
      notify:
        - pager-duty
      changed_when: True

    - name: Print all the runs of the wf
      debug: 
        var: wf_runs.json.relations.link | last

    - name: Assign the variable
      set_fact:
        last_run: "{{ wf_runs.json.relations.link | last }}"

  handlers:
    - name: pager-duty
      pagerduty_alert:
        api_key: my-key
        integration_key: xxxxx
        service_id: sss
        state: triggered
        desc: 
          - "Job started at: {{ last_run | json_query('attributes[1].value')\n }}"
          - "Job ended at: {{ last_run | json_query('attributes[2].value')\n }}"
          - "Job status: {{ last_run | json_query('attribuets[5].value')\n }}"

我想针对4个不同的url和4个不同的环境运行相同的代码,

https://myurl-env1-wf2
https://myurl-env1-wf3

我该如何实现?我曾尝试遍历URL,但在wf_runs.json.relations.lil | last处作为未定义变量失败。预先感谢

1 个答案:

答案 0 :(得分:0)

  

Q:“我想针对4个不同的网址运行相同的代码”

A:将任务放入文件中,然后添加 meta:flush_handlers 。例如

$ cat get-info-handle-allert.yml
- name: Get execution info
  uri: 
    url: "{{ item }}"
  ...
- name: Print all the runs of the wf
  ...
- name: Assign the variable
  ...
- meta: flush_handlers

循环include_tasks。例如

  tasks:
    - name: Loop get-info-handle-allert
      include_tasks: get-info-handle-allert.yml
      loop:
        - https://myurl
        - https://myurl-env1-wf2
        - https://myurl-env1-wf3

(未经测试。如果flush_handlers有问题,只需将 meta 任务替换为处理程序中的任务即可。)