简体   繁体   中英

Ansible - url check for multiple hosts

I am writing the Ansible playbook to check the URL status for multiple IPs from the hosts file, however, it is not working when I am giving input as a group name, but is working for single IP

Here is my playbook,

- hosts: "{{ hosts }}"
  vars_prompt:
    - name: "hosts"
      prompt: "Please enter the hosts details"
      private: no
    - name: "port"
      prompt: "Please enter the port"
      private: no
  serial: 1
  tasks:
  - name: check the url http://{{ hosts }}:{{ port }}/test
    uri:
      url: "http://{{ hosts }}:{{ port }}/test"
      follow_redirects: none
      method: GET
    register: _result
    until: _result.status == 200

My application is running on below Ips and I want to check the url status for all.

[webservers]
10.10.10.10
10.10.10.20
10.10.10.30
10.10.10.40

But it's getting failed with below error.

ansible-playbook test.yml -u test --ask-pass
SSH password:
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
Please enter the hosts details: webservers
Please enter the port: 8080
[WARNING]: Found variable using reserved name: port

PLAY [webservers] ****************************************************************************> ********************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [10.10.10.10]

TASK [http://webservers:8080/test]  **********************************************************************
[WARNING]: The value True (type bool) in a string field was converted to u'True' (type string). If this does not look like what
you expect, quote the entire value to ensure it does not change.
fatal: [10.10.10.10]: FAILED! => {"changed": false, "content": "", "elapsed": 0, "failed_when_result": true, "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno -2] Name or service not known>", "redirected": false, "status": -1, "url": "http://webservers:8080/test"}

PLAY RECAP **********************************************************************************************************************
10.10.10.10             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Can someone please suggest on this?

Thanks!

I think I've already solved what you're trying to do, in another question.

Please take a look at this question and answer .

You could edit the provided playbook to match your use-case.

I have written this but for webservers group it;s taking hosts as webservers only and not picking up the IP.

  • hosts: "{{ hosts }}"
    vars_prompt:
  • name: "hosts" prompt: "Please enter the host details"
    private: no
  • name: "port"
    prompt: "Please enter the port details"
    private: no
    vars:
    sites: - http://{{ hosts }}:{{ port }}/test

tasks:

  • name: verify if all urls works properly
    uri:
    url: "{{ item }}"
    timeout: 10
    with_items: "{{ sites }}"
    register: health_check
    ignore_errors: yes

But again this failed with below error. TASK [verify if all urls works properly] **************************************************************************************** failed: [10.10.10.30] (item=webservers:8080/test) => {"ansible_loop_var": "item", "changed": false, "content": "", "elapsed": 0, "item": "webservers:8080/test", "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno -2] Name or service not known>", "redirected": false, "status": -1, "url": "webservers:8080/test"}

I am not sure what I am doing wrong here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM