0%

ansible-inventory-plugins

ansible inventory 插件

查看inventory-plugins列表

1
ansible-doc -t inventory -l

以vultr为例,查看其帮助文档和样例

1
ansible-doc -t inventory vultr

inventory_vultr.yml(配置文件必须以vultr.yml或者vultr.yaml结尾)

1
2
3
plugin: vultr
api_account: test@gmail.com
api_key: 33PGQLTDVPllRALLLLLLEVX24SAKBD4SNWWQ

配置文件命名规范是代码里vultr.py固化的

1
2
3
4
5
6
165     def verify_file(self, path):
166 valid = False
167 if super(InventoryModule, self).verify_file(path):
168 if path.endswith(('vultr.yaml', 'vultr.yml')):
169 valid = True
170 return valid

测试ansible-inventory --list -i inventory_vultr.yml(或ansible-inventory -i inventory_vultr.yml --graph)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@VM-0-11-ubuntu ~ #ansible-inventory --list -i inventory_vultr.yml
{
"_meta": {
"hostvars": {
"jpvps": {
"allowed_bandwidth_gb": 1000,
"ansible_host": "156.179.95.98",
"auto_backup_enabled": false,
"cost_per_month": 5.0,
"current_bandwidth_gb": 2.122,
"date_created": "2016-05-31 04:33:20",
"default_password": "@s8MS_XDjeA*])_C",
"disk": "Virtual 25 GB",
"id": "37651598",
"internal_ip": "",
"kvm_url": "https://my.vultr.com/subs/vps/novnc/api.php?data=djJ8R0x0QkszS2H15XVatql1se1ZQnO_EMqWKZMholJirM_8mUBzKYOL4Otw6rURpdCPzWcUji9pv8wMqKqyuhPsTaZhzIzX-171KVcPFTapn9VfFryC88xKIefBL-1wIDNRQu5e",
...
...
}

默认属于vultr

1
2
3
4
5
root@VM-0-11-ubuntu ~ #ansible-inventory -i inventory_vultr.yml --graph
@all:
|--@ungrouped:
|--@vultr:
| |--jpvps

inventory_vultr.yml增加分组的一些配置

1
2
3
4
5
6
7
groups:
dev: "'vps' in name"
keyed_groups:
- separator: ""
key: os | lower
- prefix: region
key: region | lower
1
2
3
4
5
6
7
8
9
10
11
root@VM-0-11-ubuntu ~ #ansible-inventory -i inventory_vultr.yml --graph
@all:
|--@dev:
| |--jpvps
|--@region_tokyo:
| |--jpvps
|--@ubuntu_20_04_x64:
| |--jpvps
|--@ungrouped:
|--@vultr:
| |--jpvps

测试groups走了一些弯路,官方文档只给出了参数说明Add hosts to group based on Jinja2 conditionals.没给出样例

定位思路: /usr/lib/python2.7/dist-packages/ansible/plugins/inventory/vultr.py

1
2
207             # Complex groups based on jinja2 conditionals, hosts that meet the conditional are added to group
208 self._add_host_to_composed_groups(self.get_option('groups'), server, server['name'], strict=strict)

pdb下断点: /usr/lib/python2.7/dist-packages/ansible/plugins/inventory/__init__.py

1
2
3
4
5
6
7
8
9
367     def _add_host_to_composed_groups(self, groups, variables, host, strict=False):
368 ''' helper to create complex groups for plugins based on jinja2 conditionals, hosts that meet the conditional are added to group'''
369 # process each 'group entry'
370 if groups and isinstance(groups, dict):
371 variables = combine_vars(variables, self.inventory.get_host(host).get_vars())
372 self.templar.available_variables = variables
373 for group_name in groups:
374 import pdb;pdb.set_trace()
375 conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name]
1
2
3
4
5
6
root@VM-0-11-ubuntu ~ #ansible-inventory -i inventory_vultr.yml --graph
> /usr/lib/python2.7/dist-packages/ansible/plugins/inventory/__init__.py(375)_add_host_to_composed_groups()
-> conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name]
(Pdb) groups[group_name]
u"'vps' in name"
(Pdb)