ansible详解-playbook

playbook介绍

playbook是由一个或多个play组成的列表,ansible能够直接调用playbook,按照事先定义好的规则在远程主机一次执行playbook上的操做。ansible的playbook是采用YAML的形式,文件后缀为.yaml,须要遵循YAML的语法进行定义。web

注意:
playbook一个-name只能有一个模块被调用,不然会报错服务器

playbook的核心元素

  • tasks:任务,由模块定义的操做的列表
  • variables:变量
  • templates:模板,即便用了模板语法的文本文件
  • handlers:由特定条件触发的tasks
  • roles:角色

playbook的基础组件

  • hosts:指明要运行playbook的主机或者主机组
  • remote_user:指明以远程主机哪一个用户的身份运行playbook
  • sudo_user:非管理员须要拥有的sudo权限
  • tasks:指定具体要执行的任务列表

playbook经常使用命令:

ansible-playbook --syntax-check user.yml    #测试语法是否正确
ansible-playbook -C user.yml                        #测试执行,并非真正运行
ansible-playbook user.yml                             #运行playbook

playbook变量使用方法

  1. 使用内建变量facts
  2. 自定义变量:
    1. 命令行传递变量:
      -e VAR=VALUE
    2. 在inventoy中为每一个主机定义专门的变量:
    [test]
    192.168.189.129 user=test_3
    调用方法:
    \- hosts: test remote_user: root tasks: \- name: add {{ user }} #经过{{ 变量名 }}调用变量 user: name={{ user }} state=present system=yes
    1. 在inventory中为每一个主机组中的主机传递相同的变量:
    [test]
    192.168.189.129
    [test:vars]
    user=test_4
    1. 在playbook中定义:
    \ - hosts: test
      remote_user: root
      vars:
     \  - user: test_5
      \ - password: 1234567a
      tasks:
      \ - name: add {{ user }}
          user: name={{ user }} state=present system=yes password={{ password }}
    1. inventory中使用参数:
    • ansible_ssh_host
    • ansible_ssh_port
    • ansible_ssh_user
    • ansible_ssh_pass
    • ansible_sudo_pass
    [test]
    192.168.189.129 ansible_ssh_pass=1234567a

playbook中handlers的使用

- hosts: test
  vars:
    - service: httpd
  remote_user: root
  tasks:
    - name: install {{ service }}
      yum: name={{ service }} state=latest
    - name: install configure file
      copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
    - name: start {{ service }}
      service: name= {{ service }} state=started enabled=yes

  handlers:
    - name: restart httpd
      service: name=httpd  state=restarted

playbook中when的用法

- hosts: test
  remote_user: root
  tasks:
    - name: print hello world
      copy: content='hello world' dest=/tmp/test.log
      when: facter_ipaddress == '192.168.189.129'    #只有当条件知足的时候才执行此tasks

playbook中的迭代用法

playbook常常须要重复执行某指令,此时可使用迭代,迭代固定用法以下:ssh

- hosts: test
  remote_user: root
  tasks:
    - name: del user
      user: name={{ item }} state=present    #此处固定必须为item,表示调用迭代对象
      with_items:    #此处固定为with_items,下面一次列出迭代对象
        - test_2
        - test_3
        - test_4
        - test_5

也能够采用字典列表的方式来进行item调用:测试

- hosts: all
  remote_user: root
  tasks:
  - name: create groups
    group: name={{ item }} state=present
    with_items:
   - groupx1
    - groupx2
    - groupx3
    - name: create users
    user: name={{ item.name }} group={{ item.group }} state=present
    with_items:
    - {name: 'userx1', group: 'groupx1'}
    - {name: 'userx2', group: 'groupx2'}
    - {name: 'userx3', group: 'groupx3'}

playbook的templates使用

template文件可使用变量来代替某些固定的值,这样能够针对不一样的服务器提供不一样的配置文件。
变量支持以下类型:命令行

  • 主机变量
  • 主机组变量
  • facts
- hosts: test
  vars:
    - service: httpd
  remote_user: root
  tasks:
    - name: install {{ service }}
      yum: name={{ service }} state=latest
    - name: install configure file
      template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf    #template文件能够用j2结尾,表明是jinjia2文件
      notify: restart httpd
    - name: start {{ service }}
      service: name= {{ service }} state=started enabled=yes

  handlers:
    - name: restart httpd
      service: name=httpd  state=restarted

playbook中tags的应用

能够对某一个task打上tag标签,这样在执行playbook的时候能够直接指定要执行的tag,就不用总体在把全部的task再执行一遍了rest

- hosts: test
  vars:
    - service: httpd
  remote_user: root
  tasks:
    - name: install {{ service }}
      yum: name={{ service }} state=latest
    - name: install configure file
      template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
      tags: conf    #此处的conf表示tag名
    - name: start {{ service }}
      service: name= {{ service }} state=started enabled=yes

  handlers:
    - name: restart httpd

调动tag的命令:code

ansible-playbook httpd.yml -t conf
ansible-playbook httpd.yml --tags conf

playbook中roles的应用

roles能够对多个playbook进行复用,好比说A主机须要安装httpd,B主机也须要安装httpd,可是他们又没有在一个主机组,这个时候正常来讲须要写两遍playbook,就形成了重复现象。此时能够采用roles,对两个主机都指定role为httpd所在的roles便可进行playbook的复用。
roles有特定的格式,首先须要有一个roles目录,而后在目录下面须要有各个role的主目录,主目录下面为各个分目录:对象

  • roles
    • site.yml:此文件用于调用须要role文件
    • 项目目录
      • tasks:存放须要执行的任务,其下面必须有一个main.yml文件用于定义各个任务
        • main.yml
      • files:用于存放各个须要供copy模块调用的静态文件
      • template:用于存放模板文件
      • handlers:存放触发器文件,下面必须有main.yml用于定义各个触发器
        • main.yml
      • meta:能够新建main.yml文件,设置该role和其余role的关联关系
      • vars:定义须要调用的变量,须要有main.yml文件
        • main.yml
      • default:设定默认变量的时候使用此目录中的main.yml
#site.yml文件,下面文件表明给test配置websers何dbsers的role
- hosts: test
  remote_user: root
  roles:
  - websers
  - dbsers
#tasks下main.yml文件
- name: install httpd
  yum: name=httpd state=latest
- name: install config
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf    #template和copy模块的src能够直接调用相对路径,相对roles/websers/files
  notify: restart httpd
  tags: conf
- name: start httpd
  service: name=httpd state=started enabled=yes
#handlers下的main.yml文件
- name: restart httpd
  service: name=httpd state=restarted
相关文章
相关标签/搜索