コンテンツへスキップ

server.yml

hostname: server1

network: 
- eno1: 
  device: eno1  
  ipaddress: 192.168.1.1
  netmask: 255.255.255.0
  gateway: 192.168.1.254
- eno2:
  device: eno2 
  ipaddress: 192.168.1.2
  netmask: 255.255.255.0
  gateway: 192.168.1.254

 

読み込み

>>> with open('E:\\my-document\\Programing\\testinfra\\servers\\server1.yaml','r') as yml:
...     config = yaml.safe_load(yml)
... 
>>> config
{'hostname': 'server1', 'network': [{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}, {'eno2': None, 'device': 'eno2', 'ipaddress': '192.168.1.2', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}]}
>>> config['hostname']
'server1'
>>> config['network']
[{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}, {'eno2': None, 'device': 'eno2', 'ipaddress': '192.168.1.2', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}]
>>> config['network'][0]
{'eno1': None, 'device': 'eno1', 'ipaddress': '192.168.1.1', 'netmask': '255.255.255.0', 'gateway': '192.168.1.254'}
>>> config['network'][0]['ipaddress']
'192.168.1.1'
>>> config['network'][1]['ipaddress'] 
'192.168.1.2'
>>>