1

I am trying fetch to distinct groups in the inventory through a variable.this is the command am trying to run in the playbook to add hosts to the Nagios XI. Am trying to do this using Rest API through CURL command. Am getting error as Incorrect pattern. Can some one please advise about the issue. or help me out with how we can call two groups from the inventory in the same command.

- name: add host to nagios XI.
      shell: curl -XPOST "http://16.231.22.60/nagiosxi/api/v1/config/host?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name={{ item.hostname }}&address={{ item.address }}&use=xiwizard_ncpa_host&max_check_attempts=5&check_period=xi_timeperiod_24x7&notification_interval=60&notification_period=xi_timeperiod_24x7&notifications_enabled=0&contacts=nagiosadmin&contact_groups=Candle Admins,Candle-L1-L2-Internal&applyconfig=1"
      with_items:
        - { hostname: "{{ groups['grp1'] }}", address: "{{ groups['grp2'] }}"}

EDIT: code formatting

ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • Supposing you have a list of host names and a list of ip address in the inventory, you have to use with_together. See https://docs.ansible.com/ansible/2.5/plugins/lookup/together.html – imjoseangel Apr 25 '18 at 18:24

1 Answers1

1

Understanding that your hostname and address from each group match each other you can do the following:

Inventory:

[grp1]
host1
host2
host3

[grp2]
10.100.10.1
10.100.10.2
10.100.10.3

Play:

---
- name: Debug Together
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Add host to nagios XI
      shell: shell: curl -XPOST "http://16.231.22.60/nagiosxi/api/v1/config/host?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name={{ item.0 }}&address={{ item.1 }}&use=xiwizard_ncpa_host&max_check_attempts=5&check_period=xi_timeperiod_24x7&notification_interval=60&notification_period=xi_timeperiod_24x7&notifications_enabled=0&contacts=nagiosadmin&contact_groups=Candle Admins,Candle-L1-L2-Internal&applyconfig=1"
      with_together:
        - "{{ groups['grp1'] }}"
        - "{{ groups['grp2'] }}"

You will get something like:

TASK [debug] ******************************************************************************************************************
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host1', u'10.100.10.1')"
}
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host2', u'10.100.10.2')"
}
ok: [localhost] => (item=None) => {
    "item.0,  item.1": "(u'host3', u'10.100.10.3')"
}

Comming from my test:

- name:
  debug:
    var: item.0,  item.1
  with_together:
    - "{{ groups['grp1'] }}"
    - "{{ groups['grp2'] }}"
imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • Thanks much.. Its working. I have never thought of with_together module. –  Apr 26 '18 at 09:29
  • done..! I have a question for you. I am writing a disk usgae task in the same playbook through restapi. my challenge here is first the task should search for the number of drives and later apply the service. I am wondering if we can call a separate script for searching the drives and later apply the service. can you please advice.. –  Apr 26 '18 at 15:07
  • Yes, you can search the disks, create a list and then apply the service with with_items. If you want to count only, you can count the items in the list and apply the service. – imjoseangel Apr 26 '18 at 16:23
  • Am trying to delete the service from the below command curl -XDELETE "http://16.231.22.60/nagiosxi/api/v1/config/service? apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name={{ item }}&service_description=dcosNet_service but its not working can you please advise the right one. error: 400 Bad Request

    Bad Request

    your browser sent a request that this server could not understand.

    –  Apr 30 '18 at 03:40