Ansible uses dynamic inventory with cmdb
Hao Chaoyang's DevOps perspective
Ansible之inventory
Comrades who use Ansible for batch management in production should know that Ansible uses inventory files to manage assets. A common situation in production is to maintain more than one inventory file. But after having an asset management system, it is not very good to maintain a bunch of inventory files. It is impossible to guarantee the validity of each inventory file.
Ansible inventory is divided into static and dynamic. The static inventory is the file that needs to be maintained manually. Dynamic inventory is not to generate an inventory file, but to pass the obtained results directly to the relevant commands of Ansible. Today I will share with you the method of dynamically obtaining inventory in cooperation with cmdb.
Scripting
Since it cooperates with the company cmdb (developed by me and can communicate), it calls the relevant API of cmdb, so only part of the code is given. The overall idea is to realize that must support two parameters --list and --host hostname/ip. And the output format must be json format.
--list: Used to return all host information, including host information for each host group. At the same time, it can also support children, vars, _meta, etc.
--host hostname/ip: used to return a specific host list
# vim gethosts.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
import sys
import argparse
...以上忽略....
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--list',action='store_true',dest='list',help='get all hosts')
parser.add_argument('--host',action='store',dest='host',help='get specific host')
args = parser.parse_args()
if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
GroupList()
elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
HostList(sys.argv[2])
Run script
#python gethosts.py --help
usage: gethosts.py [-h] [--list] [--host HOST]
optional arguments:
-h, --help show this help message and exit
--list get all hosts
--host HOST get specific host
# python gethosts.py --list
{
"all": {
"hosts": [
"node1",
"node2",
"node3",
"node4",
"node5",
"node6",
"node7",
"node8",
"node9",
"node10",
"node11",
"node12",
"node13",
"node14"
]
}
}
# python gethosts.py --host node14
{
"ansible_ssh_host": "node14"
}
# chmod +x gethosts.py
Work with Ansible to get the host list
Get a specific host
# ansible -i gethosts.py node6 --list-hosts
hosts (1):
node6
Get all hosts
# ansible -i gethosts.py all --list-hosts
hosts (14):
node1
node2
node3
node4
node5
node6
node7
node8
node9
node10
node11
node12
node13
node14
Ansible uses dynamic inventory to execute commands
Single host
# ansible -i gethosts.py node6 -m ping
node6 | SUCCESS => {
"changed": false,
"ping": "pong"
}
All hosts
# ansible -i gethosts.py all -m ping
node1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node6 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node7 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node8 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node9 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node10 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node11 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node12 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node13 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node14 | SUCCESS => {
"changed": false,
"ping": "pong"
}
It is not so orderly in actual operation.
Actual operation diagram
Designated host
All hosts
Dynamic inventory can also be used in Ansible's api (although I don't want to mention Ansible's api).