Once we have our Ansible environment ready, next create a project. I will create a new project to demonstrate ansible roles example:
To create ansible role, use ansible-galaxy init <role_name> to create the role directory structure. We will create the role inside our /roles directory i.e. ~/nagiosxi-hostclone/roles
[root@ansbile nagiosxi-hostclone~]$ mkdir roles
[root@ansible ~]$ cd roles
Next use ansible-galaxy init command to create ansible role. We will create nagiosxi-hostclone role:
[root@ansible roles]$ ansible-galaxy init roles
- roles was created successfully
[root@ansible roles]$ tree roles
nagiosxi-hostclone
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
8 directories, 8 files
[root@ansible roles]$ cat tasks/main.yml
---
# tasks file for agiosxi-hostclone
- name: Create Nagiosxi file
template:
src: "{{ templet }}"
dest: "/usr/local/nagios/etc/import/{{ fqdn }}.cfg"
notify: import nagios config
notify: Restart the nagios config
Next we will create the template content which will be used to update host clonning in our ansible roles examples. I will create a new template file under templates directory using some variables:
[root@ansible roles]$ cat templates/nagios-hosts.cfg.j2
###############################################################################
#
# Hosts configuration file
#
# Created by: Nagios CCM 3.0.6
# Date: 2020-07-19 12:38:25
# Version: Nagios Core 4.x
#
# --- DO NOT EDIT THIS FILE BY HAND ---
# Nagios CCM will overwrite all manual settings during the next update if you
# would like to edit files manually, place them in the 'static' directory or
# import your configs into the CCM by placing them in the 'import' directory.
#
###############################################################################
define host {
host_name {{ fqdn }}
use xiwizard_linuxsnmp_host
address {{ fqdn_address }}
max_check_attempts 1
check_interval 5
retry_interval 5
check_period xi_timeperiod_24x7
contacts nagiosadmin
notification_interval 0
notification_period xi_timeperiod_24x7
first_notification_delay 0
notification_options d,u,r,f,
notifications_enabled 1
register 1
}
###############################################################################
#
# Hosts configuration file
#
# END OF FILE
#
###############################################################################
We will use defaults folder to define custom variables which is used in our template file templates/<templete.cfg.j2>
[root@ansible roles]$ cat defaults/main.yml
---
fqdn: "{{ fqdn }}"
fqdn_address: "{{ fqdn_address }}"
# Step 5 : Remove unwanted directories (Optional)
This step is completely optional. In this ansible roles example we will not use other directories so we are deleting them. After deleting the additional directories you can use tree command to list the directory structure of agiosxi-hostclone roles
copy
[root@ansible roles]$ rm -rf tests vars
[root@ansible roles]$ tree
.
├── README.md
├── add-nagiosxi-monitoring.yml
├── ansible.cfg
├── gitlab
│ ├── README.md
│ └── ansibleplaybook-git
├── inventory
└── roles
└── nagiosxi-monitoring
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
├── nagios-hosts.cfg.j2
├── nagiosxi-hosts.cfg.j2
├── nagiosxi-ja-services.cfg.j2
└── nagiosxi-servicest.cfg.j2
10 directories, 13 files
Now after you create ansible role structure, we need a playbook file which will deploy the role to our managed hosts. I will create my playbook file add-nagiosxi-monitoring.yml under nagiosxi-hostclone project directory.
[root@ansible nagiosxi-hostclone]$ cat add-nagiosxi-monitoring.yml
---
- name: Nagiosxi host using agiosxi-hostclone
hosts: all
user: root
become: true
roles:
- role: nagiosxi-monitoring
==========================================================================================================================================================
- Nagiosxi-Monitoring:
- This also role interacts with: The Nagios server specified with the nagios_server variable
- Nagios Host and Nagios Services are configured for target host
- Expects fact gathering
---
- name: Nagiosxi host clonning using Asnsible playbook
hosts: all
user: root
become: true
roles:
- role: nagiosxi-monitoring
ansible-playbook -e "fqdn=<FQDN Host Name> fqdn_address=<IP Address for host> templet=<Templet to clone the host and services>" add-nagiosxi-monitoring.yml
ansible-playbook -e "fqdn=batfs-21-hyd.jonnalas.com fqdn_address=192.168.0.103 templet=nagiosxi-hosts.cfg.j2" add-nagiosxi-monitoring.yml
ansible-playbook -e "fqdn=batfs-21-hyd.jonnalas.com fqdn_address=192.168.0.103 templet=nagiosxi-service.cfg.j2" add-nagiosxi-monitoring.yml