自动化运维Ansible之playbook剧本的使用

YAML简介

YAML是一种表达资料序列的格式,由于参考了其他多种语言,所以具有很高的可读性。其特性如下:

  • 具有很好的可读性,易于实现
  • 表达能力强,扩展性好
  • 和脚本语言的交互性好
  • 有一个一致的信息模型
  • 可以基于流来处理

1.YAML中两种常用的数据类型,分别是list和directory

  • list
    -teacher
    -student

    2.列表的所有元素均使用“-”开头

  • directory

3.字典通过key和value进行标识如:

name:zhangsan
job:teacher
age:25

playbook介绍

playbook是由一个或者多个play组成的列表,主要功能是将task定义好的角色并为一组进行统一管理,也就是通过task调用Ansible的模块将多个paly组织在一个playbook中。playbook本身由以下各部分组成:

  • Tasks:任务,即调用模块完成的某操作
  • Varibles:变量
  • Templates:模版
  • Handlers:处理器,当某条件,满足时,触发的操作
  • Roles:角色

1.下面是一个playbook的简单示例:

- hosts: webserver              #定义的主机组,即应用的主机
  vars:                        #定义变量
    http_port: 80
    max_clients: 200
  user: root
  tasks:                               #执行的任务
  - name: ensure apache is at the latest version
    yum: pkg=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running
    service: name=httpd state=started
  handlers:                       #处理器
    - name: restart apache
      service: name=httpd state=restarted

Hosts和Users介绍

playbook的设计目的就是为了让某个或者某些主机以某个身份去执行相应的任务。其中用于指定要执行任务的主机hosts定义,可以是一个主机也可以是由冒号分隔的额多个主机组;用于指定被管理主机上执行任务的用户用remote_user来定义,例如:

- hosts: webserver
  remote_user: root

1.remote_user也可以定义指定用户通过sudo的方法在被管理主机上运行指令,甚至可以在使用become指定sudo切换的用户。

- hosts: webserver
  remote_user: root
  tasks:
  - name: ping test
    ping:
    become: yes
    become_user: zhangsan 

[root@rabbitmq01 ~]# ansible-playbook a.yml 

PLAY [webserver] *********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [192.168.58.132]

TASK [ping test] *********************************************************************************
 [WARNING]: Module remote_tmp /home/zhangsan/.ansible/tmp did not exist and was created with a
mode of 0700, this may cause issues when running as another user. To avoid this, create the
remote_tmp dir with the correct permissions manually

ok: [192.168.58.132]

PLAY RECAP ***************************************************************************************
192.168.58.132             : ok=2    changed=0    unreachable=0    failed=0   

tasks列表和action

1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。
在运行playbook时(从上到下执行),如果一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,然后重新执行即可。
Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量.

2.每一个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。如果没有定义name,‘action’的值将会用作输出信息中标记特定的task。

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

tasks:
- name: make sure appache is running
  service: name=httpd state=started

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

tasks:
-name: disable selinux
command: /sbin/setenforce 0

handlers介绍

Handlers用于当关注的资源发生变化时所采取的操作。在notify中列出的操作便称为handler,也就是在notify中需要调用handler中定义的操作。而notify这个动作在每个play的最后被触发,仅在所有的变化发生完成后一次性执行指定的操作。handler也是task列表的格式:

notify:
 - restart httpd  #一旦执行这里就会触发name为restart httpd的handler
handlers:
 - name: restart httpd
   service: name=httpd state=restarted

Templates介绍

Jinja是基于Python的模板引擎。Template类是Jinja的另一个重要组件,可以看作一个编译过的模板文件,用于产生目标文本,传递Python的变量给模板去替换模板中的标记。这里就先演示一个示例,通过ansible在两台被管理主机上安装httpd服务,并且修改httpd.conf文件后,重启。前面的安装已经介绍过,这里我直接说明下实验环境即可:

角色 主机名 IP地址 组名
控制主机 node1 192.168.58.146
被管理主机 node2 192.168.58.148 webserver
被管理主机 node3 192.168.58.149 sqlserver
- hosts: webserver
  remote_user: root
  vars:   #定义几个变量
   - httpdport: 192.168.58.148:80
   - servername: www.yun.com:80
   - service: httpd
  tasks:
   - name: install httpd service
     yum: name={{service}} state=latest
   - name: install configuration file for httpd
     template: src=/root/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
     # 首先在本地生成一份template文件
     notify:
      - restart httpd
  handlers:
    - name: restart httpd
      service: name={{service}} state=restarted

1.这个是yml文件内容,下面时模板文件中需要修改的内容

[root@node1 ~]# cat httpd.conf.j2 | grep '^Listen'
Listen {{httpdport}}

[root@node1 ~]# cat httpd.conf.j2 | grep '^ServerName'
ServerName {{servername}}
[root@node1 ~]# ansible-playbook abc.yml 

PLAY [webserver] **********************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [192.168.58.148]

TASK [install httpd service] **********************************************************************
changed: [192.168.58.148]

TASK [install configuration file for httpd] *******************************************************
changed: [192.168.58.148]

RUNNING HANDLER [restart httpd] *******************************************************************
changed: [192.168.58.148]

PLAY RECAP ****************************************************************************************
192.168.58.148             : ok=4    changed=3    unreachable=0    failed=0   

2.可以看到node2上服务已经开启,监听端口也已经修改过来了

[root@node2 .ssh]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64
[root@node2 .ssh]# netstat -ntap | grep 80
tcp        0      0 192.168.58.148:80       0.0.0.0:*               LISTEN      3540/httpd
[root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^Listen'
Listen 192.168.58.148:80
[root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^ServerName'
ServerName www.yun.com:80

Tags介绍

如果多次执行修改playbook会涉及到一些没有变化的代码,可以使用tags让用户选择跳过没有变化的代码,只运行olaybook中发生变化的部分代码,可以在playbook中为某个或者某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。

- hosts: sqlserver
  remote_user: root
  tasks:
   - name: build a new file
     copy: content="this is a test" dest=/root/test1.txt
     tags:
      - only
   - name: bulid another file
     copy: content="this is another test" dest=/root/test2.txt
[root@node1 ~]# ansible-playbook a.yml --tags="only"

PLAY [sqlserver] *********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [192.168.58.149]

TASK [build a new file] **************************************************************************
changed: [192.168.58.149]

PLAY RECAP ***************************************************************************************
192.168.58.149             : ok=2    changed=1    unreachable=0    failed=0  

我们去node3中查看到底生成了几个文件

[root@node3 ~]# ls
anaconda-ks.cfg       mfs-1.6.27-5.tar.gz  公共  视频  文档  音乐
initial-setup-ks.cfg  test1.txt            模板  图片  下载  桌面

可以看到只生成了test1.txt

猜你喜欢

转载自blog.51cto.com/13642258/2156934