@nalan90
2018-06-20T10:20:23.000000Z
字数 1306
阅读 3000
Python高效编程技巧实战
yaml文件操作
In [23]: cat init-server.yml
roles:
jdy-web:
public_ports:
- '20000/tcp'
- '20003/tcp'
web:
public_ports:
- '80/tcp'
- '443/tcp'
squid:
public_ports:
- '3128/tcp'
dns:
public_ports:
- '53/udp'
- '53/tcp'
log:
public_ports:
- '601/tcp'
- '514/udp'
- '5044/tcp'
kv_config:
vintage_ss_bypass_ips: '47.91.129.91 47.52.7.116 172.168.100.0/24 139.162.107.188 47.89.58.99/22 47.90.40.63/22'
ci:
jenkins_http_port: 11881
gitlab_url: 'https://git.sunfund.com'
gitlab_runner_token: 'Y2ixs1d1Zdq7o_4gZdtk'
gitlab:
gitlab_http_port: 11180
rsync:
ssh_port: 11222
server: 'bak-01.ns.9dy.in'
----------
In [15]: import yaml
## yaml将yml格式的文件处理成一个dict object
In [16]: with open('init-server.yml','r') as f:
....: data = yaml.load(f)
In [17]: print type(data)
<type 'dict'>
In [24]: data
Out[24]:
{'kv_config': {'ci': {'gitlab_runner_token': 'Y2ixs1d1Zdq7o_4gZdtk',
'gitlab_url': 'https://git.sunfund.com',
'jenkins_http_port': 11881},
'gitlab': {'gitlab_http_port': 11180},
'rsync': {'server': 'bak-01.ns.9dy.in', 'ssh_port': 11222},
'vintage_ss_bypass_ips': '47.91.129.91 47.52.7.116 172.168.100.0/24 139.162.107.188 47.89.58.99/22 47.90.40.63/22'},
'roles': {'dns': {'public_ports': ['53/udp', '53/tcp']},
'jdy-web': {'public_ports': ['20000/tcp', '20003/tcp']},
'log': {'public_ports': ['601/tcp', '514/udp', '5044/tcp']},
'squid': {'public_ports': ['3128/tcp']},
'web': {'public_ports': ['80/tcp', '443/tcp']}}}