import yaml
with open("./test.yaml") as f:
x = yaml.load(f)
print(x)
[{'tasks': [{'yum': {'state': 'latest', 'name': 'httpd'}, 'name': 'ensure apache is at the latest version'}, {'name': 'write the apache config file', 'template': {'dest': '/etc/httpd.conf', 'src': '/srv/httpd.j2'}, 'notify': ['restart apache']}, {'name': 'ensure apache is running', 'service': {'state': 'started', 'name': 'httpd'}}], 'hosts': 'webservers', 'remote_user': 'root', 'vars': {'http_port': 80, 'max_clients': 200}, 'handlers': [{'name': 'restart apache', 'service': {'state': 'restarted', 'name': 'httpd'}}]}]
test.yaml
---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:
- name: restart apache
service:
name: httpd
state: restarted
-----------------------------------------------------------------
import yaml
d = [{'tasks': [{'yum': {'state': 'latest', 'name': 'httpd'}, 'name': 'ensure apache is at the latest version'}, {'name': 'write the apache config file', 'template': {'dest': '/etc/httpd.conf', 'src': '/srv/httpd.j2'}, 'notify': ['restart apache']}, {'name': 'ensure apache is running', 'service': {'state': 'started', 'name': 'httpd'}}], 'hosts': 'webservers', 'remote_user': 'root', 'vars': {'http_port': 80, 'max_clients': 200}, 'handlers': [{'name': 'restart apache', 'service': {'state': 'restarted', 'name': 'httpd'}}]}]
with open("./test.yaml","w") as f:
yaml.dump(d,f)
test.yaml
- handlers:
- name: restart apache
service: {name: httpd, state: restarted}
hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: {name: httpd, state: latest}
- name: write the apache config file
notify: [restart apache]
template: {dest: /etc/httpd.conf, src: /srv/httpd.j2}
- name: ensure apache is running
service: {name: httpd, state: started}
vars: {http_port: 80, max_clients: 200}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步