Explore the fleet of machines with Ansible
Ansible becomes more and more popular automation tool. It has no agent (technically it has sshd, python and bash but such a software is present on any machine), zero initial deployment, very easy to start.
Problem definition
Configure hardware watchdog on the fleet of machine. We don’t know what the hardware, what the operation systems and we have no proper discovery tool to get this information.
Ansible key features
We are going to use following Ansible features
Host grouping module group_by
Creates list of host groups for every unique key value.
Debug module debug
Writes some output to your ssh console
1
2
- name: Unsupported configuration
debug: msg="Configuration {{ansible_product_name}} {{ansible_distribution}}-{{ansible_distribution_version}} is not supported yet"
Host list management
Ansible supports some sort of set operations on host list
- All hosts means all following tasks will be performed to all available hosts in inventory
1
- hosts: all
- Reference to host group. Here we are creating new host groups based on previosly created one.
1
2
3
4
5
6
7
8
9
- hosts: all
tasks:
- name: Create a group of all hosts by hardware
group_by: key={{ansible_product_name}}
- hosts: PowerEdge-R720xd
tasks:
- name: Create a group of all hosts by operating system
group_by: key={{ansible_distribution}}-{{ansible_distribution_version}}
- Union
1
2
3
- hosts: Ubuntu-14.04:Ubuntu-12.04
tasks:
...
- Complement
1
2
3
- hosts: all:!Ubuntu-14.04:!Ubuntu-12.04:!CentOS-7.1.1503
tasks:
...
All together
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
---
- hosts: '{{ target }}'
tasks:
- name: Create a group of all hosts by hardware
group_by: key={{ansible_product_name}}
- hosts: PowerEdge-R720xd
tasks:
- name: Create a group of all hosts by operating system
group_by: key={{ansible_distribution}}-{{ansible_distribution_version}}
- hosts: CentOS-7.1.1503
tasks:
- name: Configure watchdog
[...]
- hosts: Ubuntu-14.04:Ubuntu-12.04
tasks:
- name: Configure watchdog
[...]
- hosts: all:!Ubuntu-14.04:!Ubuntu-12.04:!CentOS-7.1.1503
tasks:
- name: Unsupported configuration
debug: msg="Configuration {{ansible_product_name}} {{ansible_distribution}}-{{ansible_distribution_version}} is not supported yet"
So, starting with no host group by ansible_product_name we are exploring what hardware is present in our fleet, with adding operation systems groups and substructing them from latest host group we have less and less unknown configurations. As result this playbook could be improved incrementally with adding new types of configurations.
Sample output