开发动态的Inventory数据源:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import json
a={
"all": [
"192.168.1.6",
"192.168.1.10"
]
}
print a
print type(a)
print a['all']
print type(a['all'])
b=json.dumps(a)
print b
print type(b)
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a20.py
{'all': ['192.168.1.6', '192.168.1.10']}
<type 'dict'>
['192.168.1.6', '192.168.1.10']
<type 'list'>
{"all": ["192.168.1.6", "192.168.1.10"]}
<type 'str'>
测试:
[root@node01 ~]# cat /etc/ansible/inventory.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
a={
"all": [
"192.168.137.3"
]
}
b=json.dumps(a)
print b
[root@node01 ~]# python -i /etc/ansible/inventory.py --list
{"all": ["192.168.137.3"]}
>>> exit()
[root@node01 ~]# python /etc/ansible/inventory.py --list
{"all": ["192.168.137.3"]}
[root@node01 ~]# python /etc/ansible/inventory.py --hosts
{"all": ["192.168.137.3"]}
[root@node01 ~]# ansible -i /etc/ansible/inventory.py all -m raw -a "hostname"
192.168.137.3 | SUCCESS | rc=0 >>
node2
Shared connection to 192.168.137.3 closed.