Creating Ansible role for services addition to nagiosXI - ansible

I am trying to add a service to the NagiosXI with the below CURL command.
curl -k -XPOST "https://16.231.22.60/nagiosxi/api/v1/config/service?apikey=qfOQpKFORCNo7HPunDUsSjW7f2rNNmrdVv3kvYpmQcNdSS2grV2jeXKsgbv3QgfL&pretty=1" -d "host_name=***{{ item }}***&***service_description=Service status for: sshd***&use=xiwizard_ncpa_service&check_command=check_xi_ncpa\! -t 5nidNag -P 5693 -M services -q service=sshd,status=running&check_interval=5&retry_interval=1"
In the above command only hostname and service description description changes. I am calling hostname with Item module. and adding service description manually. if i need to add 50 services i neeed to write this command for 50 times.
i am planning to write it by ansible roles. can someone help me out with this.

You can do something like:
---
- name: Nagios Config
gather_facts: False
hosts: localhost
vars:
servers:
- 10.100.10.5
- 10.100.10.6
- 10.100.10.7
services:
- ssh
- https
- smtp
tasks:
- name: Add Nagios services
debug:
msg: "curl -host {{item.0}} with service {{ item.1 }}"
with_nested:
- "{{ servers }}"
- "{{ services }}"
Getting the following output:
TASK [Add Nagios services] ********************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.5 with service ssh"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.5 with service https"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.5 with service smtp"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.6 with service ssh"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.6 with service https"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.6 with service smtp"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.7 with service ssh"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.7 with service https"
}
ok: [localhost] => (item=None) => {
"msg": "curl -host 10.100.10.7 with service smtp"
}
Try the uri module if it doesn't fit your requirements, go for the shell one. I have reflected the debug one just to answer the question.

Related

How to set bool var based on if string or list contains specified value?

Please tell me what I am doing wrong here. I have these Ansible tasks defined in my role:
- meta: end_play
when:
- pp_srv is not defined
- name: Debug pp_srv splitted.
debug:
msg: "pp_srv spilti up is {{ pp_srv.split(',') }}"
- name: Set Monitoring Host
ansible.builtin.set_fact:
cd_mon_host: "{{ groups.monitor.0 }}"
when:
- cd_mon_host is not defined
- name: Debug Show pp_srv
debug:
var: pp_srv
- name: Check Usage - Set is_cd_host to False
ansible.builtin.set_fact:
is_cd_host: False
- name: Check Usage - Set is_cd_host to True
ansible.builtin.set_fact:
is_cd_host: True
when:
- pp_srv | search("diags")
- name: Debug is_cd_host
debug:
msg: "is_cd_host is {{ is_cd_host }}"
- name: Assert if
assert:
that: false
When I run molecule test or run a playbook that using this role I get this output ...
TASK [example.diags : Debug pp_srv splitted.] ******************
ok: [cd-host-01] => {
"msg": "pp_srv spilti up is ['all', 'agger', 'esync', 'apiconf', 'diags']"
}
ok: [cd-host-02] => {
"msg": "pp_srv spilti up is ['apiconf']"
}
ok: [mon-host-01] => {
"msg": "pp_srv spilti up is ['all', 'apiconf', 'diags']"
}
TASK [example.diags : Set Monitoring Host] ******************
ok: [cd-host-01]
ok: [cd-host-02]
ok: [mon-host-01]
TASK [example.diags : Debug Show pp_srv] ***********************
ok: [cd-host-01] => {
"pp_srv": "all,agger,esync,apiconf,diags"
}
ok: [cd-host-02] => {
"pp_srv": "apiconf"
}
ok: [mon-host-01] => {
"pp_srv": "all,apiconf,diags"
}
TASK [example.diags : Check Usage - Set is_cd_host to False] ***
ok: [cd-host-01]
ok: [cd-host-02]
ok: [mon-host-01]
TASK [example.diags : Check Usage - Set is_cd_host to True] ****
ok: [cd-host-01]
ok: [cd-host-02]
ok: [mon-host-01]
TASK [example.diags : Debug is_cd_host] ************************
ok: [cd-host-01] => {
"msg": "is_cd_host is True"
}
ok: [cd-host-02] => {
"msg": "is_cd_host is True"
}
ok: [mon-host-01] => {
"msg": "is_cd_host is True"
}
I had hoped that is_cd_host is False for host cd-host-02 because it does not have the string, diags
in the variable pp_srv.
What did I do wrong?
UPDATE:
Thanks β.εηοιτ.βε
I followed your advice and I my role is working fine now.
I have this now:
- name: Check Usage - Set is_cd_host.
ansible.builtin.set_fact:
is_cd_host: "{{ 'diags' in pp_srv.split(',') }}"

Define which users belong to which host

I have an inventory:
[dbs]
server1.intranet
[webservices]
server2.intranet
[apps]
server3.intranet
And a file with variables:
users:
- { name: user1, ssh_key: <SSH_KEY> }
- { name: user2, ssh_key: <SSH_KEY> }
(1) My first question is: How can I tell in the inventory which user is part of each server? (without having to copy and duplicate the user information at every host) Note that users can change and can belong to multiple servers.
(2) The final objective is to do some tasks at each host. For example, to create the users at each host, and add the corresponding user SSH key at each server, something like:
- name: SSH
ansible.posix.authorized_key:
user: "item.name"
state: present
key: "item.ssh_key"
with_items: "{{ users[??] }}"
Of course the users variable should only have the users for the specific host iterating.
How can I do this?
I didn't understand your second point, but this solution could be helpful.
Define destination hosts as an array:
users:
- { name: user1, ssh_key: <SSH_KEY>,hosts: ['test-001','test-002'] }
- { name: user2, ssh_key: <SSH_KEY>,hosts: ['test-002'] }
Use selectattr filter for your loop to search the running hostname in the hosts list defined in the vars:
- name: SSH
ansible.posix.authorized_key:
user: "{{ item.name }}"
state: present
key: "{{ item.ssh_key }}"
loop: "{{ users | selectattr('hosts', 'search', inventory_hostname) }}"
ok: [test-001] => (item={'name': 'user1', 'ssh_key': '<SSH_KEY1>', 'hosts': ['test-001', 'test-002']}) => {
"msg": "user1"
}
ok: [test-002] => (item={'name': 'user1', 'ssh_key': '<SSH_KEY1>', 'hosts': ['test-001', 'test-002']}) => {
"msg": "user1"
}
ok: [test-002] => (item={'name': 'user2', 'ssh_key': '<SSH_KEY2>', 'hosts': ['test-002']}) => {
"msg": "user2"
}

Ansible Tower: Custom Credential Type

I created a Custom Credential in Ansible Tower and need to use it in a role.
The credential name is custom_cred -> this has 2 keys custom username and custom password.
I've tried hostvars[inventory_hostname][custom_cred]['custom username'] but its not working.
To debug your Custom Credential Types you could use
- hosts: localhost
gather_facts: yes
tasks:
- name: Get environment
debug:
msg: "{{ ansible_env }}"
resulting into an output of
TASK [Get environment] *********************************************************
ok: [localhost] => {
"msg": [
{
...
"custom_username": "username",
"custom_password": "********",
...
}
...
if such Custom Test Credentials are configured. This is working for AWX/Tower. You can then follow up with
Ansible Tower - How to pass credentials as an extra vars to the job template?

Ansible how to run task only on groups mentioned in playbook and skip other groups even though same host part of other group

I have a role that is common for both mongo replicas and arbiters and hosts groups separately for each replica and arbiter because the role should support the arbiter on the same host & different host.
hosts:
[replicas]
127.0.0.1
127.0.0.2
[arbiter]
127.0.0.2
the task inside role:
- name: Run only on replicas
debug msg=" Only on replica"
when: '"replicas" in group_names'
- name: Run only on the arbiter
debug: msg="Only on the arbiter"
when: '"arbiter" in group_names'
playbook:
- hosts: replicas
roles:
- role: "common"
- role: "replica"
- hosts: arbiter
roles:
- role: "common"
- role: "arbiter'
Expected output while running on replicas:
TASK [debug] *********************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": " Only on replica"
}
ok: [127.0.0.2] => {
"msg": " Only on replica"
}
TASK [debug(arbiter)] *********************************************************************************************************************************************
skipping: [127.0.0.1]
skipping: [127.0.0.2]
But is not skipping on arbiter task as expected as the same host is part of replicas group. Below is the actual output.
Actual output:
TASK [debug] *********************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": " Only on replica"
}
ok: [127.0.0.2] => {
"msg": " Only on replica"
}
TASK [debug(arbiter)] *********************************************************************************************************************************************
skipping: [127.0.0.1]
ok: [127.0.0.2] => {
"msg": " Only on replica"
}
How to run on a specific group that playbook delegated?
hello you can use this method:
Playbook:
- hosts: replicas
roles:
- { role: common, vars: { group: "replicas" } }
- { role: replica, vars: { group: "replicas" } }
- hosts: arbiter
roles:
- { role: common, vars: { group: "arbiter" } }
- { role: arbiter, vars: { group: "arbiter" } }
and inside your role:
- name: Run only on replicas
debug msg=" Only on replica"
when: group == "replicas"
- name: Run only on the arbiter
debug: msg="Only on the arbiter"
when: group == "arbiter"
I hope that can help you to resolve your issue.

calling a custom variable from inventory

I have a some custom variables in my inventory file. I would like to call them in my playbook, but not sure how to do this.
--------------------------------INV--------------------------------
[testA]
namgw01a
namgw02a
[testB]
namgw01b
namgw02b
[nam:children]
testA
testB
[testA:vars]
file=file_a.conf
[testB:vars]
file=file_b.conf
--------------------------------PLAYBOOK--------------------------------
vars:
- file: "{{ file }}"
- name: "show variable"
debug:
var: file
--------------------------------RESULT--------------------------------
TASK [show variable] **************************************************************************************************************************************
ok: [namgw01b] => {
"file": "VARIABLE IS NOT DEFINED!"
}
ok: [namgw02b] => {
"file": "VARIABLE IS NOT DEFINED!"
}

Resources