fruits:
- Apple
- Orange
- Strawberry
- Mango
martin:
name: Martin D‘vloper
job: Developer
skill: Elite
需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
[root@ansible-server ~]# ansible all -m yum -a ‘name=httpd state=removed‘ -o
用于获取配置文件
[root@ansible-server ~]# yum install -y httpd
[root@ansible-server ~]# cp -rf /etc/httpd/conf/httpd.conf /tmp/
[root@ansible-server ~]# vim /tmp/httpd.conf
Listen 8080
[root@ansible-server ~]# vim /tmp/apache.yaml
- hosts: test
tasks:
- name: install latest apache packages
yum: name=httpd state=latest
- name: copy apache config file
copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure apache httpd service is running
service: name=httpd state=started enabled=yes
[root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --syntax-check
[root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --list-tasks
[root@ansible-server ~]# ansible-playbook /tmp/apache.yaml --list-hosts
[root@ansible-server ~]# ansible-playbook /tmp/apache.yaml
如果配置文件发生改变,要求服务进行重启,此时需要借助handlers的帮助:
修改配置文件
[root@ansible-server ~]# vim /tmp/httpd.conf Listen 9080
修改剧本文件
[root@ansible-server ~]# vim /tmp/apache.yaml - hosts: test tasks: - name: install latest apache packages yum: name=httpd state=latest - name: copy apache config file copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: restart apache service - name: ensure apache httpd service is running service: name=httpd state=started enabled=yes handlers: - name: restart apache service service: name=httpd state=restart
再次执行
ansible-playbook /tmp/apache.yaml
,触发handlers,重启httpd服务
原文:https://www.cnblogs.com/ElegantSmile/p/12588806.html