四、Ansible自动化运维实战-YAML-非标记语言

1.语法

叫压马路语言

  ·列表

 

2.示例

  ·需求

  通过YAML编写一个剧本,完成web的部署,配置,启动的全过程

 

  ·准备工作

  ansible webserver -m yum -a 'name=httpd state=removed' -o  删除客户机上的httpd以及相关的服务

  ansible webserver -m yum -a 'name=httpd-tools state=removed' -o  相关服务tools

  yum install -y httpd  本机下载httpd服务,准备配置文件

  mkdir apache

  cd apache

  cp -rf /etc/httpd/conf/httpd.conf .

  grep '^Listen' httpd.conf查看端口80

  vim httpd.conf

————————————

Listen 8080  # 改成8080端口

————————————

 

  ·编写剧本

  vim apache.yaml

——————————————————

- hosts: webserver
  tasks:
  - name: install apache packages for xulei
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
  - name: ensure apache is running
    service: name=httpd state=started enabled=yes

——————————————————

    0.-号后面要加空格

    1.引号后面要加空格

    2.tasks要和hosts对齐

    3.yum属于name,需要与name对齐,其他同理

 

  ·测试

  ansible-playbook apache.yaml --syntax-check  如果只返回文件名证明语法上没错误,不保证参数错误

  ansible-playbook apache.yaml --list-tasks  列出所有任务

  ansible-playbook apache.yaml --list-hosts  列出任务主机

  ansible-playbook apache.yaml  执行

 

  ·handlers触发器

  如果配置文件发生变化

  vim httpd.conf

————————————

Listen 9000  # 改成9000端口

————————————

  ansible-playbook apache.yaml

  你会发现覆盖了之前的8080,文件中listen已是9000,但是之前apache里写的是start服务,所以现在为止服务并没有停止,还是使用的8080端口,我们不能restart服务,因为如果每执行一次都重启会丢失用户

  vim apache.yaml

————————————

- hosts: webserver
  tasks:
  - name: install apache packages for xulei
    yum: name=httpd state=present
  - name: copy apache conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart apache service for xulei  # 如果copy文件发生改变则触发条件触发器,内容要和该name内容完全一致
  - name: ensure apache is running
    service: name=httpd state=started enabled=yes
  handlers:  # 条件触发器
  - name: restart apache service for xulei  # 内容
    service: name=httpd state=restarted  # 重启服务

————————————

 

 

 

 

 

 

 

 

 

  

posted @ 2022-03-14 00:37  真渡  阅读(105)  评论(0)    收藏  举报