自动化运维必备——ansible中playbook的编写

一、playbook 简介

Ansible的脚本—playbook剧本
通过task调用ansible的模板将多个play组织在一个playbook中运行。
playbooks本身由以下各部分组成:

  • (1) Tasks: 任务,即调用模块完成的某操作;
  • (2) Variables: 变量
  • (3) Templates:模板
  • (4) Handlers: 处理器,当某条件满足时,触发执行的操作;
  • (5) Roles: 角色。

Play的主体部分是task列表, task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一 个任务后再开始第二个任务,在运行playbook时(从上到下执行),如果一个host执行task失败, 整个tasks都会回滚,请修正playbook中的错误,然后重新执行即可。嵌入了事务机制,可以回滚,容错性好。

Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,模块执行时幂等的,这意味着多次执行是安全的,因为其结果一致。

每一 个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一 个task的。

定义一个task, 常见的格式:”module: options" 例如: yum: name=httpd

.ansible的自带模块中,command模块和shelI模块无需使用key=value格式

二、playbook的执行命令

ansible-playbook [yaml文件名]

例如: ansible-playbook ping.yml
参数: -k用来交互输入ssh密码 -K用来交互输入sudo密码 -u 指定用户

补充命令:

ansible-playbook nginx.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook nginx.yaml --list-task     #检查tasks任务
ansible-playbook nginx.yaml --list-hosts    #检查生效的主机
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf'
#指定从某个task开始运行

三、playbook 编写

指定远程主机sudo切换用户:

- hosts: mysql
   remote_ user: root
   become: yes
   become_ user: zzff   #无需配置ssh,sudo提权
   tasks:
    - name: copy text
       copy: src=/etc/fstab dest=/home/mysql/fstab.bak

或者:

- hosts: mysql
  remote_user: root
  tasks:
   - name: touch
     file: path=/opt/aa state=directory mode=776 owner=bb group=bb
     #设置bb用户的可执行权限
   - name: copy
     copy: src=/etc/fstab dest=/opt/aa/fstab.bak
     remote_user: bb  #配置ssh

忽略子任务的错误,继续执行,不进行回滚

- hosts: webserver
  remote user: root
  tasks:
   - name: disable selinux
     command: '/sbin/setenforce 0'
     ignore_ errors: True
      #忽略错误,强制返回成功,而不会因为一条错误直接回滚
   - name: make sure apache is running
     service: name= httpd state= started

多主机组任务

- hosts: webserver
  remote user: root
  tasks:
   - name: create nginx group
      group: name =nginx system=yes gid=208
   - name: create nginx user
      user: name=nginx uid= 208 group=nginx system=yes
- hosts: mysql
   remote user: root
   tasks:
    - name: copy file to mysq|
       copy: src=/etc/inittab dest=/opt/inittab.back

handlers的调用(类比函数的使用,function)

handlers也是一些task列表,由通知者进行notify调用,如果没有被notify调用,就不会执行handlers中的任务,如果被执行,无论有多个通知者进行了notify,等到play中的所有taslk执行完成后,handler只会被执行一次。

 - hosts: webserver
  remote_ user: root
  tasks: 
 - name: install httpd package
     yum: name=httpd state=latest
 - name: install configuration file for httpd
     copy: src =/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:        #调用
      -restart httpd
 - name: start httpd service
     service: enabled=true name= httpd state=started
  handlers:         #handles的任务列表
 - name: restart httpd
     service: name=httpd state=restarted

playbook 引入变量

  • 在yaml文件中指定
 - hosts: webserver
   remote_ user: root
   vars:
    - package: httpd
    - service: httpd    #声明变量名:变量值
   tasks:
    - name: install httpd package
      yum: name={{package}} state=latest 
    - name: install configuration file for httpd
      copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify:
       -restart httpd
    - name: start httpd service
       service: enabled =true name={{service}} state=started
   handlers:
    - name: restart httpd
      service: name= {{service}} state=restarted
  • 直接引用系统变量,无需声明
copy: content="{{ansible_ all_ipv4_ addresses}}" dest=/opt/vars.txt
变量中包含:主机中的所有IPV4地址
  • 命令行传递变量参数
ansible-playbook demo.yaml -e "package=httpd"
 //yaml文件中是 -package 空的变量,只有变量名
  • 主机变量
在主机清单设置:
vi /etc/ansible/hosts
[mysq|]
10.0.0.30  testvar="80.183"  #定义testvar变量的值为80.183
vi test.yml  #添加{{testvar}}主机变量
 - hosts: mysqI
   remote_ user: root
   tasks:
    - name: copy file
      copy: content= "{{ansible_ all ipv4_ addresses}},{{testvar}}" dest=/opt/vars.txt

条件判断

单条件
- hosts: mysq|
  remote_ user: root 
  tasks:
    - name: "shutdown CentOS"
      command: /sbin/shutdown -h now
      when: ansible_distribution == "CentOS"

多条件判断
- hosts: mysq|
   remote_ user: root
   tasks:
     - name: "shut down CentOS 7 systems"
       command: /sbin/shutdown -r now
       when:
        - ansible_distribution == "CentOS"
        - ansible_distribution major version == "7"  #必须同时满足

组条件判断
- hosts: mysql
  remote_ user: root
  tasks:
    - name: " shut down CentOS 6 and Debian 7 systems"
      command: /sbin/shutdown -t now
      when: (ansible_ distribution == "CentOS" and ansible_ _distribution major_ version == "6") or (ansible_ distribution == "Debian" and ansible distribution major_ version == "7")
      #关闭所有 centos6 和d ebian7 系统的主机

执行迭代任务,重复性的任务

利用item变量传递,with_items中存放需要遍历的元素。

- hosts: webserver
  remote_ user: root
  tasks:
   - name: "Install Packages"
     yum: name={{ tem}} state=latest   #依次安装httpd..php
     with_items:
       - httpd
       - mysql-server
       - php

也可以自定义
- hosts: webserver
  remote_ user: root
  tasks:
    - name:" Add users"
      user: name={{ item.name }} state=present groups={{ item.groups }}
      with_ items:
        - { name:'test1', groups:'wheel'}   
        - { name:'test2', groups:'root'}
        依次创建用户 test1 test2,对应组为wheel'、,root
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值