ansible--yaml

语法

#列表
fruits:
  -Apple
  -Orange
  -banada

###########################################
fruits顶格写,下面的参数空两个空格,必须得对齐
#字典

martin: 
  name: Mysql
  environoment: dd

################################
第一行的冒号号有空格,其他行的缩进要对其,冒号右边有空格,左边没有
#案例
#完成web服务的部署,配置,启动的全过程


#准备工作
ansible all -m yum -a 'name=httpd state=removed'
#先卸载
#在主机装
yum -y install httpd
mkdir apache
cd apache
cp -rf /etc/httpd/conf/httpd.conf
grep '^Listen' httpd.conf Listen 8080 #修改配置,便于推送

#############################################################
vim apache.yaml

- hosts: server2
  tasks: 
    - name: install apache package
      yum: name=httpd state=present
    - name: copy apache conf
      copy: src=./httpd.conf dest=/etc/httpd.conf/httpd.conf
    - name: ensure isrunning
      service: name=httpd state=started enabled=yes
##############################################################
- 后面有空格
#############################################################

语法检测

#价差有咩有语法错误
ansible-playbook apache.yaml --syntax-check
#列出任务
ansible-playbook apache.yaml --list-tasks

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


执行

ansible-playbook apache.yaml

handlers

如果配置文件发生变化,如Listen 8090

#执行yaml
ansible-playbook apache.yaml


#将会显示指令执行完成,配置改变了,但是访问8090不成功,
#用netstat -anpt 查看端口,还是8080

配置文件推过去,但是需要重启才能生效

vim apache.yaml

- hosts: server2
  tasks: 
    - name: install apache package
      yum: name=httpd state=present
    - name: copy apache conf
      copy: src=./httpd.conf dest=/etc/httpd.conf/httpd.conf
    - name: ensure isrunning
      service: name=httpd state=restarted enabled=yes
###########################################################################
restarted 也行,但是不推荐,真实业务环境下可能会丢失数据

#######################################################################
vim apache.yaml

- hosts: server2
  tasks: 
    - name: install apache package
      yum: name=httpd state=present
    - name: copy apache conf
      copy: src=./httpd.conf dest=/etc/httpd.conf/httpd.conf
      notify: restart apache service
    - name: ensure isrunning
      service: name=httpd state=started enabled=yes
  handlers:
    name: restart apache service
    service: name=httpd state=restarted

######################################################################3
notify时通知,如果notify动了,才会通知handlers启动重启,handlers和tasks 对齐

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值