Home > OS >  Ansible playbooks to remove multiple servers from multiple nagios monitoring via api keys
Ansible playbooks to remove multiple servers from multiple nagios monitoring via api keys

Time:12-27

I need to remove multiple servers from multiple nagios monitoring via api keys with ansible. so I tried below solution but not able to get api key section in command and get the success output

example- hosts file -->

[nagios_server]
10.0.0.1 apikey=XXXXXXXX
10.0.0.2 apikey=YYYYYYYY
10.0.0.3 apikey=ZZZZZZZZ

[Linux_remove_machine]
192.168.0.1
192.168.0.2

Main.yml -->

---
- name: remove server from monitoring
  hosts: localhost
  vars:
    output_path: "./reports/"
    filename: "device_report_{{ date }}.csv"
  tasks:
    - include_tasks: nagios.yml
      loop: "{{ groups['nagios_server'] }}"
      loop_control:
        loop_var: outer_item

      register: check
    - debug:
        var=check
        

nagios.yml -->

- name: execute command in loop
  shell:
  curl -XDELETE "http://{{ outer_item }}/nagiosxi/api/v1/config/host?apikey=**{{apikey }}**&host_name={{ item }}&applyconfig=1

  loop: "{{groups['Linux_remove_machine']}}"
  register: remove_nagios

Expected below output

 curl -XDELETE "http://10.0.0.1/......./XXXXXX/&host_name=192.168.0.1&applyconfig=1
 curl -XDELETE "http://10.0.0.2/......./YYYYYY/&host_name=192.168.0.1&applyconfig=1
 curl -XDELETE "http://10.0.0.3/......./ZZZZZZ/&host_name=192.168.0.1&applyconfig=1

 curl -XDELETE "http://10.0.0.1/......./XXXXXX/&host_name=192.168.0.2&applyconfig=1
 curl -XDELETE "http://10.0.0.2/......./YYYYYY/&host_name=192.168.0.2&applyconfig=1
 curl -XDELETE "http://10.0.0.3/......./ZZZZZZ/&host_name=192.168.0.2&applyconfig=1

CodePudding user response:

Create a dictionary of the API keys. Declare the variable

  apikey_dict: "{{ dict(groups.nagios_server|
                        zip(groups.nagios_server|
                            map('extract', hostvars, 'apikey'))) }}"

gives

  apikey_dict:
    10.0.0.1: XXXXXXXX
    10.0.0.2: YYYYYYYY
    10.0.0.3: ZZZZZZZZ

Then, use it to reference the apikey. Test it

shell> cat nagios.yml
- name: execute the command in the loop                                                                                                                                                                
  debug:                                                                                                                                                                                       
    msg: 'curl -XDELETE "http://{{ outer_item }}/nagiosxi/api/v1/config/host?apikey=**{{ apikey_dict[outer_item] }}**&host_name={{ item }}&applyconfig=1'                                      
  loop: "{{ groups.Linux_remove_machine }}"                                                                                                                                                    
  register: remove_nagios

You'll probably have to escape the quotation in the URL when running curl


Example of a complete playbook for testing

- hosts: localhost

  vars:

    apikey_dict: "{{ dict(groups.nagios_server|
                          zip(groups.nagios_server|
                              map('extract', hostvars, 'apikey'))) }}"

  tasks:

    - debug:
        var: apikey_dict

    - include_tasks: nagios.yml
      loop: "{{ groups.nagios_server }}"
      loop_control:
        loop_var: outer_item
      register: check

    - debug:
        var:
          check

gives (abridged)

  msg: curl -XDELETE "http://10.0.0.1/nagiosxi/api/v1/config/host?apikey=**XXXXXXXX**&host_name=192.168.0.1&applyconfig=1
  msg: curl -XDELETE "http://10.0.0.1/nagiosxi/api/v1/config/host?apikey=**XXXXXXXX**&host_name=192.168.0.2&applyconfig=1
  msg: curl -XDELETE "http://10.0.0.2/nagiosxi/api/v1/config/host?apikey=**YYYYYYYY**&host_name=192.168.0.1&applyconfig=1
  msg: curl -XDELETE "http://10.0.0.2/nagiosxi/api/v1/config/host?apikey=**YYYYYYYY**&host_name=192.168.0.2&applyconfig=1
  msg: curl -XDELETE "http://10.0.0.3/nagiosxi/api/v1/config/host?apikey=**ZZZZZZZZ**&host_name=192.168.0.1&applyconfig=1
  msg: curl -XDELETE "http://10.0.0.3/nagiosxi/api/v1/config/host?apikey=**ZZZZZZZZ**&host_name=192.168.0.2&applyconfig=1
  • Related