从 yaml 文件生成 ansible 主机清单 python
produce ansible hosts inventory from yaml file python
我在 yaml 文件 sot-inventory.yaml
中存储了真实来源
- Hostname: NY-SW1
IP mgmt address: 10.1.1.1
OS: IOS
Operational status: Install
Role of work: Switch
Site: New_York
- Hostname: MI-SW1
IP mgmt address: 11.1.1.1
OS: NX-OS
Operational status: Install
Role of work: Switch
Site: Maiami
- Hostname: PA-SW1
IP mgmt address: 12.1.1.1
OS: Arista
Operational status: Install
Role of work: Witch
Site: Paris
我想使用 python 脚本从上面的文件中获取 yaml ansible 主机清单,如下所示:
hosts.yaml
---
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
'role of work': switch
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nxos
'role of work': switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
'role of work': switch
有人可以提供建议 - 哪种 python 结构或 scrypt 示例可能有助于自动化此员工?
不同的方式
用 PyYaml 解析 YAML
安装 pip install pyyaml
然后
import yaml
with open("sot-inventory.yaml", "r") as stream:
try:
print(yaml.safe_load(stream))
except yaml.YAMLError as exc:
print(exc)
正则表达式老派方法
您可以使用具有多个匹配和命名组的正则表达式,并以您想要的格式打印输出
示例:
r"- Hostname: (?P<hostname>.*)\n\s*IP mgmt address: (?P<ip>.*)\n"gm
将创建 2 个组(组主机名和组 IP)的 3 个匹配项。
查看组的工作原理:https://docs.python.org/3/howto/regex.html#grouping
您可以在这里尝试您的正则表达式:https://regex101.com/r/7Gz3JQ/2
如果您遇到困难,请不要犹豫,将您的脚本粘贴到此处。我们会帮忙的。
例如,Ansible 剧本
- hosts: localhost
tasks:
- set_fact:
sot: "{{ lookup('file', 'sot-inventory.yaml')|from_yaml }}"
- template:
src: hosts.yaml.j2
dest: hosts.yaml
和模板
shell> cat hosts.yaml.j2
---
{% for i in sot|groupby('Site') %}
{{ i.0|lower }}:
hosts:
{% for j in i.1 %}
{{ j['Hostname']|lower }}:
ansible_host: {{ j['IP mgmt address'] }}
os: {{ j['OS']|lower }}
'role of work': {{ j['Role of work']|lower }}
{% endfor %}
{% endfor %}
创建文件hosts.yaml
shell> cat hosts.yaml
---
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nx-os
'role of work': switch
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
'role of work': switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
'role of work': witch
测试一下
shell> ansible-inventory -i hosts.yaml --list --yaml
all:
children:
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nx-os
role of work: switch
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
role of work: switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
role of work: witch
ungrouped: {}
我在 yaml 文件 sot-inventory.yaml
中存储了真实来源- Hostname: NY-SW1
IP mgmt address: 10.1.1.1
OS: IOS
Operational status: Install
Role of work: Switch
Site: New_York
- Hostname: MI-SW1
IP mgmt address: 11.1.1.1
OS: NX-OS
Operational status: Install
Role of work: Switch
Site: Maiami
- Hostname: PA-SW1
IP mgmt address: 12.1.1.1
OS: Arista
Operational status: Install
Role of work: Witch
Site: Paris
我想使用 python 脚本从上面的文件中获取 yaml ansible 主机清单,如下所示:
hosts.yaml
---
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
'role of work': switch
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nxos
'role of work': switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
'role of work': switch
有人可以提供建议 - 哪种 python 结构或 scrypt 示例可能有助于自动化此员工?
不同的方式
用 PyYaml 解析 YAML
安装 pip install pyyaml
然后
import yaml
with open("sot-inventory.yaml", "r") as stream:
try:
print(yaml.safe_load(stream))
except yaml.YAMLError as exc:
print(exc)
正则表达式老派方法
您可以使用具有多个匹配和命名组的正则表达式,并以您想要的格式打印输出
示例:
r"- Hostname: (?P<hostname>.*)\n\s*IP mgmt address: (?P<ip>.*)\n"gm
将创建 2 个组(组主机名和组 IP)的 3 个匹配项。
查看组的工作原理:https://docs.python.org/3/howto/regex.html#grouping
您可以在这里尝试您的正则表达式:https://regex101.com/r/7Gz3JQ/2
如果您遇到困难,请不要犹豫,将您的脚本粘贴到此处。我们会帮忙的。
例如,Ansible 剧本
- hosts: localhost
tasks:
- set_fact:
sot: "{{ lookup('file', 'sot-inventory.yaml')|from_yaml }}"
- template:
src: hosts.yaml.j2
dest: hosts.yaml
和模板
shell> cat hosts.yaml.j2
---
{% for i in sot|groupby('Site') %}
{{ i.0|lower }}:
hosts:
{% for j in i.1 %}
{{ j['Hostname']|lower }}:
ansible_host: {{ j['IP mgmt address'] }}
os: {{ j['OS']|lower }}
'role of work': {{ j['Role of work']|lower }}
{% endfor %}
{% endfor %}
创建文件hosts.yaml
shell> cat hosts.yaml
---
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nx-os
'role of work': switch
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
'role of work': switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
'role of work': witch
测试一下
shell> ansible-inventory -i hosts.yaml --list --yaml
all:
children:
maiami:
hosts:
mi-sw1:
ansible_host: 11.1.1.1
os: nx-os
role of work: switch
new_york:
hosts:
ny-sw1:
ansible_host: 10.1.1.1
os: ios
role of work: switch
paris:
hosts:
pa-sw1:
ansible_host: 12.1.1.1
os: arista
role of work: witch
ungrouped: {}