ansible中的剧本playback详解

目录

1、 ansible的剧本-playbook

2、playbook的语法结构

语法 yaml 格式配置

 3、ansible语法解析-template模块

1、 ansible的剧本-playbook

如果ansible的各个模块(能实现各种功能)是车间里的各个工具;playbook就是指导手册,目标远程主机就是库存和原料对象。从根本上说playbook和shell脚本没有任何区别,playbook就像shell一样,也是把一堆一堆的命令组合起来,然后加入对应条件判断等等,在shell脚本中是一条一条的命令,而在playbook中是一个人的任务构成的,每个任务可以看做是shell脚本中的一条命令;shell脚本一般只是在当前服务器上执行,而playbook则不止是在一个服务器上执行,因此playbook需要在其中指定运行该playbook的服务器名。

2、playbook的语法结构

playbook使用yaml标记语言,这种标记语言在文件的最开始需要使用三个“-”来说明文件开始,然后使用缩进来说明代码块的范围。

语法 yaml 格式配置

1、playbook的核心元素:

> hosts :playbook配置文件作用的主机
> tasks:任务列表
> variables:变量
> templates:包含模板语法的文本文件
> handlers:由特定条件触发的任务。
> roles:用于层次性、结构化地组织playbook。
> roles能根据层次型结构自动装载变量文件、tasks以及handlers等。

2、playbook运行方式 

**ansible-playbook --check** :检测模式,playbook中定义的所有任务将在每台主机上检测,但是并不真的执行。
**ansible-playbook --list-hosts** :列出运行任务的主机
**ansible-playbook --syntax-check playbook.yaml** :语法检测
**ansible-playbook -t TAGS_NAME playbook.yaml** :只执行TAGS_NAME任务
**ansible-playbook playbook.yaml**: 运行

 3、playbook的使用

---                             #标记文件的开始

- hosts: webservers             #指定该playbook在哪个服务器上执行

  vars:                         #表示下面是定义的变量,

    http_port: 80          #变量的形式,key: value,这里http_port是变量名,80是值

    max_clients: 200

  remote_user: root     #指定远程的用户名,这里缩进和vars保持了一致,说明变量的代码块已经结束。

  tasks:              #下面构成playbook的tasks,每个task都有 - name: 开始,name指定该任务的名称。

  - name: ensure apache is at the latest version  #指定该任务的名称。
    
    #yum说明要是用的模板名称,后面指定对应的参数,这两行结合起来就相当于一个shell命令。
    yum: pkg=httpd state=latest                   

  - name: write the apache config file            #每个task之间可以使用空行来做区分。

    template: src=/srv/httpd.j2 dest=/etc/httpd.conf  

例如:① 创建yaml文件

[root@b ~]# cat ansible_playbook_sc.yaml 
- hosts: webserver   #或者all
  remote_user: root
  tasks:
  - name: upload file
    copy: src=/etc/passwd dest=/tmp/passwd_tmp
  - name: ip get
    shell: ip a
- hosts: nginx
  tasks:
  - name: shell tasks
    shell: cat /etc/passwd

② 可以使用python解析yaml

>>> fp = open("ansible_playbook_sc.yaml")
>>> dict = yaml.load(fp)
>>> dict
[{'hosts': 'all', 'remote_user': 'root', 'tasks': [{'name': 'up file', 'copy': 'src=/etc/passwd dest=/tmp/passwd_tmp'}]}]

③ 执行playbook:ansible-playbook 文件名

[root@sc-master ~]# ansible-playbook ansible_playbook_sc.yaml 
[WARNING]: Could not match supplied host pattern, ignoring: webserver

PLAY [webserver] ***************************************************************
skipping: no hosts matched

PLAY [webserver] ***************************************************************
skipping: no hosts matched

PLAY RECAP *********************************************************************

上传和删除文件。 

root@b ~]# cat ansible_playbook_sc.yaml 
- hosts: all
  remote_user: root
  tasks:
  - name: up file
    copy: src=/etc/passwd dest=/tmp/passwd_tmp
  - name: download redis
    yum: name=redis state=installed
- hosts: webser
  tasks:
  - name: remove file
    shell: rm -rf /tmp/passwd_tmp

例如:使用playbook去部署web服务:安装nginx,并启动nginx。

- hosts: all
  remote_user: root
  tasks:
  - name: yum nginx
    yum: name=nginx state=installed
  - name: copy sc.conf
    copy: src=/lianxi/sc.conf dest=/etc/nginx/conf.d/sc.conf
  - name: copy index
    copy: src=/lianxi/index.html dest=/opt/dist/
  - name: start nginx
    service: name=nginx state=started

 3、ansible语法解析-template模块

templa是基于jinja2模板语法去解析的。根据一定的条件灵活的设置要复制文件中的部分关键内容,可以使用template模块。template模板在运用Template模板在运用时与copy模块类似,区别在于可以在Ansible的Playbook执行的时候,根据一定的条件灵活的设置要复制文件中的部分关键内容。同时,Ansible的Playbook中关于Template的使用支持条件判断、循环、逻辑运算、比较等内容,增强了配置的灵活性。

注意:Templat模板使用jinjia2语言进行配置,模板文件必须以j2结尾。

测试实例:如在ansible机器上安装nginx,然后将nginx的配置中的listen中listen_port的设置为变量,可以将这个变量根据自己的需求进行定义、设置。首先是先编写j2文件(下面为 sc_template.conf.j2),里面定义listen_port变量,然后编写一个剧本(template_test.yaml);然后执行文件剧本,指定listen_port变量的值。

① 编写 vim sc_template.conf.j2模本文件。

[root@scmysql opt]# cat sc_template.conf.j2 
server {
        listen       {{ listen_port }} ;
        server_name  www.sc.com;
        root         /opt/dist;
    access_log  /var/log/nginx/sc_access.log  main;
        location / {
    }
    location =/api {
    }
}

{{ listen_port }}:用双括号定义了一个listen_port变量,我们需要在调用Playbook时对该变量进行赋值,然后该变量就会替代模板文件中的变量。 

② 编写测试剧本。剧本模板,一个模板一个剧本。(剧本里的内容为将存放在/opt目录下的sc_template.conf.j2模板文件复制到远程B、C机器上的/tmp/sc.conf文件中)

[root@scmysql opt]# cat template_test.yaml 
- hosts: web
  tasks:
  - name: template test
    template: src=/opt/sc_template.conf.j2 dest=/tmp/sc.conf

③ 执行playbook, 指定listen_port变量的值。-e 用来指定需要listen_port变量的值为8080。

ansible-playbook template_test.yaml -e "listen_port=8080"

[root@scmysql opt]# ansible-playbook template_test.yaml -e "listen_port=8080"


方式二:采用个性化模板:也可以在hosts文件中 (/etc/ansible/hosts )文件里面配置 指定每一台主机的listen变量。

[root@scmysql ansible]# cat hosts
[web]
192.168.2.132:22 listen_port=81   # 指明监听的端口为81
192.168.2.137  listen_port=666

[nginx]
192.168.2.11   # 随便测试的一个代码

 例题二:①测试在ansible机器上的编写test.j2文件,定义{{ name }}的值为可以更改的变量。

[root@sc-master opt]# cat test.j2
#########################################################################
# File Name: test.sh
# Description: test.sh
# Author: lkong
# mail: lkong@tencent.com
# Created Time: 2022-08-21 17:24:54
#########################################################################
#!/bin/bash
hello  {{ name }}
shanshan shi zhu

 ② 编写剧本:template_test.yaml,将A:ansible机器上的这个文件(存放在/opt/test.j2)复制到远程B、C机器上的/lianxi/test.sh文件中。

[root@sc-master opt]# cat template_test.yaml
- hosts: webser
  tasks:
  - name: text test
    template: src=/opt/test.j2 dest=/lianxi/test.sh

③ 执行剧本,并-e 指定name变量的值为sanchuang。-e "name=sanchuang".

[root@sc-master opt]# ansible-playbook template_test.yaml -e "name=sanchuang"
[WARNING]: Found variable using reserved name: name

PLAY [webser] *****************************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [192.168.2.137]
ok: [192.168.2.132]

TASK [text test] **************************************************************************
changed: [192.168.2.137]
changed: [192.168.2.132]

PLAY RECAP ********************************************************************************
192.168.2.132              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.137              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

查看B、C机器上的文件:

B机器:

[root@sc-slave lianxi]# ls
ansible  test.sh
[root@sc-slave lianxi]# cat test.sh
#########################################################################
# File Name: test.sh
# Description: test.sh
# Author: lkong
# mail: lkong@tencent.com
# Created Time: 2022-08-21 17:24:54
#########################################################################
#!/bin/bash
hello  sanchuang
shanshan shi zhu

C机器上:

[root@nginx-kafka03 lianxi]# ls
aa  ansible  bb  cc  file_num.sh  lianxi  test.sh  tongle  xieshan
[root@nginx-kafka03 lianxi]# cat test.sh
#########################################################################
# File Name: test.sh
# Description: test.sh
# Author: lkong
# mail: lkong@tencent.com
# Created Time: 2022-08-21 17:24:54
#########################################################################
#!/bin/bash
hello  sanchuang
shanshan shi zhu

B、C机器上存有这个文件,并且name变量的值也为:sanchuang。就说明执行成功。

方式二:也可以去 使用个性化的方式:① 直接在/etc/ansible/hosts 文件里面配置name变量的值为sangchuang。

 [root@sc-master opt]# vim /etc/ansible/hosts
 25 [webser]                        
 26 192.168.2.132:22  name=sanchuang
 27 192.168.2.137:22 

然后在执行剧本:ansible-playbook template_test.yaml -e "name=sanchuang"

也可以生效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值