Skip to content

Instantly share code, notes, and snippets.

@cinek810
Last active June 27, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cinek810/e41fac186bbcd7645875833b97b1b372 to your computer and use it in GitHub Desktop.
Save cinek810/e41fac186bbcd7645875833b97b1b372 to your computer and use it in GitHub Desktop.
#Check gist description on https://funinit.wordpress.com/2018/06/27/ansible-role-used-as-plugin-for-simple-rest-api-services/
icinga2_api_uri: https://your.host:your_port/v1/objects/services/
icinga2_api_user: XXX
icinga2_api_password: XXX
icinga2_validate_certs: no
#Check gist description on https://funinit.wordpress.com/2018/06/27/ansible-role-used-as-plugin-for-simple-rest-api-services/
- hosts:
- usinkok-acc01
roles:
- icinga2-service
vars:
service:
name: TestService2
state: present
command: PassiveCheck
#Check gist description on https://funinit.wordpress.com/2018/06/27/ansible-role-used-as-plugin-for-simple-rest-api-services/
dependencies:
- { role: icinga2-service, service: { state: present, command: PassiveCheck, name: TestService2}
#Check gist description on https://funinit.wordpress.com/2018/06/27/ansible-role-used-as-plugin-for-simple-rest-api-services/
- name: fail if started with unsupported state
fail: msg="service state has to be one present or deleted"
when: service["state"] is not defined or service["state"] not in [ "present" , "deleted" ]
- name: get_information_about_service
check_mode: no
register: icinga2Service
changed_when: false
local_action:
module: uri
url: '{{icinga2_api_uri + ansible_hostname + "!" + service["name"]}}'
method: GET
force_basic_auth: yes
user: '{{icinga2_api_user}}'
password: '{{icinga2_api_password}}'
validate_certs: '{{icinga2_validate_certs}}'
status_code: 200,404
- name: create_service
changed_when: true
register: serviceCreated
when: icinga2Service.status == 404 and service["state"] == "present"
local_action:
module: uri
url: '{{icinga2_api_uri + ansible_hostname + "!" + service["name"]}}'
method: PUT
body: '{"templates": [ "generic-service" ],
"attrs": {
"display_name": "{{service["name"]}}",
"check_command" : "{{service["command"]}}",
"vars.critical": "0",
"check_interval": "{{service["check_interval"] | default(600)}}",
"host_name": "{{ansible_hostname}}"
}
}'
body_format: json
force_basic_auth: yes
user: '{{icinga2_api_user}}'
password: '{{icinga2_api_password}}'
validate_certs: '{{icinga2_validate_certs}}'
status_code: 200
headers:
Content-Type: "application/json"
Accept: "application/json"
- name: update_service
when: service["state"] == "present" and serviceCreated.skipped == true
register: resultUpdate
local_action:
module: uri
url: '{{icinga2_api_uri + ansible_hostname + "!" + service["name"]}}'
method: POST
body: '{"templates": [ "generic-service" ],
"attrs": {
"display_name": "{{service["name"]}}",
"check_command" : "{{service["command"]}}",
"vars.critical": "0",
"check_interval": "{{service["check_interval"] | default(600)}}",
"host_name": "{{ansible_hostname}}"
}
}'
body_format: json
force_basic_auth: yes
user: '{{icinga2_api_user}}'
password: '{{icinga2_api_password}}'
validate_certs: '{{icinga2_validate_certs}}'
status_code: 200
headers:
Content-Type: "application/json"
Accept: "application/json"
- name: delete_service
changed_when: true
register: result
when: icinga2Service.status == 200 and service["state"] == "deleted"
local_action:
module: uri
url: '{{icinga2_api_uri + ansible_hostname + "!" + service["name"]}}?cascade=1'
method: DELETE
force_basic_auth: yes
user: '{{icinga2_api_user}}'
password: '{{icinga2_api_password}}'
validate_certs: '{{icinga2_validate_certs}}'
status_code: 200
headers:
Content-Type: "application/json"
Accept: "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment