05Ansible_YAML

YAML语法

列表

fruits:
  - Apple
  - Orange
  - Strawberry
  - Mango

字典

martin:
  name: Martin D'vloper
  job: Developer
  skill: Elite

Ansible YAML入门

需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。

准备工作

清理环境

[root@ansible-server ~]# ansible all -m yum -a 'name=httpd state=removed' -o

安装测试httpd

用于获取配置文件

[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

访问网页

网址:http://172.22.69.216:8080

如果配置文件发生改变,要求服务进行重启,此时需要借助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服务

posted on 2020-03-28 19:04  D小调的微笑  阅读(156)  评论(0编辑  收藏  举报