其他分享
首页 > 其他分享> > 六、动态主机分组

六、动态主机分组

作者:互联网

•无限可能
–Ansible Inventory实际上是包含静态Inventory和劢态Inventory两部分,静态Inventory指的是在文件/etc/ansible/hosts中指定的主机和组,Dynamic Inventory指通过外部脚本获取主机列表,并按照ansible所要求的格式返回给ansilbe命令的。
•json
–JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基亍文本,独立亍诧言的轻量级数据交换格式。
•注意事项:
–1、主机部分必须是列表格式的;[web1,web2,...webn]
–2、hostdata行,其中的"hosts" 部分可以省略,但如果使用时,必须是"hosts"
•脚本输出主机列表
#!/usr/bin/python
import json
hostlist= {}
hostlist["bb"] = ["192.168.1.15", "192.168.1.16"]
hostlist["192.168.1.13"] = {
"ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
}
hostlist["aa"] = {
"hosts" : ["192.168.1.11", "192.168.1.12"],
"vars" : {
"ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
}
}
print(json.dumps(hostlist))
•脚本输出样例
{
"aa" : {
"hosts" : ["192.168.1.11", "192.168.1.12"],
"vars" : {
"ansible_ssh_user" : "root",
"ansible_ssh_pass" : "pwd"
}
},
"bb" : ["192.168.1.15", "192.168.1.16"],
"192.168.1.13": { "ansible_ssh_user" : "root",
"ansible_ssh_pass" : "pwd"}
}
例如:
1.编写脚本
[root@ansible csansible]# vim aaa.sh
#!/bin/bash
echo '
{
"web" : ["web1","web2"],
"db" : ["db1","db2"],
"other" : ["cache"]
}'
~
[root@ansible csansible]# sh aaa.sh

{
"web" : ["web1","web2"],
"db" : ["db1","db2"],
"other" : ["cache"]
}
[root@ansible csansible]# vim ansible.cfg

[defaults]
inventory = aaa

标签:主机,192.168,ansible,分组,ssh,hosts,动态,root,Inventory
来源: https://www.cnblogs.com/momo6656/p/15124036.html