课时6 实现playbook高级应用和企业级实战

该xx.yml脚本已经执行了,当一个配置文件修改了后,再次执行xx.yml文件,由于服务并不会再次启动
优化:当配置文件一旦发生变化,服务立即重启
课时6 实现playbook高级应用和企业级实战
handlers是一个触发器,跟tasks是同级,监控tasks下的一个动作
notify 通知handlers,用于tasks下的某个任务标记
在notify下可以多个引发触发器
vim httpd.yml

  • hosts: websrvs
    remote_user: root

    tasks:

    • name: install package httpds
      yum: name=httdp
    • name: copy config file
      copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
    • name: start service
      service: name=httpd state=started enabled=yes

保存退出
执行:
ansible-playbook -C httpd.yml
ansible-playbook httpd.yml
验证:
ansible websrvs -m shell -a ‘ss -nlt| gerp :80’

修改httpd.conf文件中的端口为8080,再次执行发现,修改了配置文件,还是未生效,端口还是80
优化:当配置文件一旦发生变化,服务立即重启

课时6 实现playbook高级应用和企业级实战
killall -0 就是探测服务是否启动,$?得出非0,就是有问题
vim httpd.yml

  • hosts: websrvs
    remote_user: root

    tasks:

    • name: install package httpds
      yum: name=httdp
    • name: copy config file
      copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
      notify: restart service
    • name: start service
      service: name=httpd state=started enabled=yes
      handlers:
      - name: restart service
      service: name=httpd state=restarted
    • name: xxxxx
      xxxxx: xxxxx
      ···················································
      tags 标签使用
      课时6 实现playbook高级应用和企业级实战
      tags就是个其中一个action起个名,标签,添加标签后
      在执行命令时,添加该标签名,则执行该标签的action
      ansible-playbook -t rshttpd,inshttpd httpd.yml
      且多个相同标签,该标签的action都会执行
      –list-tags 列出有标签的标签名
      ·························································
      使用变量:
      课时6 实现playbook高级应用和企业级实战
      setup记录了远程主机的各种状态信息,并被调用变量
      ansible websrvs -m setup -a ‘filter=ansible_hostname’ filter过滤 支持通配符
      在playbook中可以定义/调用变量
      vars:
    • var1: value1

vim app.yml

  • hosts: appsrvs
    remote_user: root

    tasks:

    • name: install package
      yum: name={{ pkname }}
    • name: start service
      service: name={{ pkname }} state=started enabled=yes
      保存退出

赋值变量且执行
ansible-playbook -e ‘pkname=vsftpd pkname2=vsftpd’ app.yml 该vsftpd服务端口是21