【问题标题】:Ansible: How to check an URL with uri module and loop?Ansible:如何使用 uri 模块和循环检查 URL?
【发布时间】:2022-12-01 18:21:50
【问题描述】:

I'm trying to check URL's with the uri module and a loop of course because I have more than one URL.

---
- name: check URLs with a loop
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    url:
      - https://www.google.com
      - https://example.com
      - https://www.wikipedia.org

 tasks:
   - name: test url
     uri:
       url: "{{ item }}"
     loop:
      - "{{ url }}"

【问题讨论】:

  • Since this seems to be description of what you are trying to do only, can you describe in more detail what is the question, or which problems or errors you are observing?

标签: loops ansible uri


【解决方案1】:

You may have a look into the following minimal example.

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Check that a page returns a status 200
    uri:
      url: "{{ item }}"
      return_content: yes
    register: this
    loop:
      - http://www.example.com
      - http://www.example.org

  - name: Show content
    debug:
      msg: "{{ item.content }}"
    loop_control:
      label: "{{ item.url }}"
    loop: "{{ this.results }}"

resulting into an output of

TASK [Check that a page returns a status 200] *****
ok: [localhost] => (item=http://www.example.com)
ok: [localhost] => (item=http://www.example.org)

TASK [Show content] *******************************
ok: [localhost] => (item=http://www.example.com) =>
  msg: |-
    <!doctype html>
    ...
ok: [localhost] => (item=http://www.example.org) =>
  msg: |-
    <!doctype html>
    ...

As you see can from the debug task the data structure of the result set changed in compare to the former given example. Therefore it is recommend to get familiar with it by using additional tasks like

- name: Show var
    debug:
      var: this.results

Further Documentation

【讨论】:

    猜你喜欢
    • 2022-12-20
    • 1970-01-01
    • 2022-11-18
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多