ansible详解-playbook

playbook介绍

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

注意:
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 }}
5. 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经常需要重复执行某指令,此时可以使用迭代,迭代固定用法如下:

- 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再执行一遍了

- 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的命令:

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
原文地址:https://www.cnblogs.com/stacks/p/8545531.html