• 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的使用

    1. --- #标记文件的开始
    2. - hosts: webservers #指定该playbook在哪个服务器上执行
    3. vars: #表示下面是定义的变量,
    4. http_port: 80 #变量的形式,key: value,这里http_port是变量名,80是值
    5. max_clients: 200
    6. remote_user: root #指定远程的用户名,这里缩进和vars保持了一致,说明变量的代码块已经结束。
    7. tasks: #下面构成playbook的tasks,每个task都有 - name: 开始,name指定该任务的名称。
    8. - name: ensure apache is at the latest version #指定该任务的名称。
    9. #yum说明要是用的模板名称,后面指定对应的参数,这两行结合起来就相当于一个shell命令。
    10. yum: pkg=httpd state=latest
    11. - name: write the apache config file #每个task之间可以使用空行来做区分。
    12. template: src=/srv/httpd.j2 dest=/etc/httpd.conf

    例如:① 创建yaml文件

    1. [root@b ~]# cat ansible_playbook_sc.yaml
    2. - hosts: webserver   #或者all
    3.   remote_user: root
    4.   tasks:
    5.   - name: upload file
    6.     copy: src=/etc/passwd dest=/tmp/passwd_tmp
    7.   - name: ip get
    8.     shell: ip a
    9. - hosts: nginx
    10.   tasks:
    11.   - name: shell tasks
    12.     shell: cat /etc/passwd

    ② 可以使用python解析yaml

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

    ③ 执行playbook:ansible-playbook 文件名

    1. [root@sc-master ~]# ansible-playbook ansible_playbook_sc.yaml 
    2. [WARNING]: Could not match supplied host pattern, ignoring: webserver
    3. PLAY [webserver] ***************************************************************
    4. skipping: no hosts matched
    5. PLAY [webserver] ***************************************************************
    6. skipping: no hosts matched
    7. PLAY RECAP *********************************************************************

    上传和删除文件。 

    1. root@b ~]# cat ansible_playbook_sc.yaml 
    2. - hosts: all
    3.   remote_user: root
    4.   tasks:
    5.   - name: up file
    6.     copy: src=/etc/passwd dest=/tmp/passwd_tmp
    7.   - name: download redis
    8.     yum: name=redis state=installed
    9. - hosts: webser
    10.   tasks:
    11.   - name: remove file
    12.     shell: rm -rf /tmp/passwd_tmp

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

    1. - hosts: all
    2.   remote_user: root
    3.   tasks:
    4.   - name: yum nginx
    5.     yum: name=nginx state=installed
    6.   - name: copy sc.conf
    7.     copy: src=/lianxi/sc.conf dest=/etc/nginx/conf.d/sc.conf
    8.   - name: copy index
    9.     copy: src=/lianxi/index.html dest=/opt/dist/
    10.   - name: start nginx
    11.     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模本文件。

    1. [root@scmysql opt]# cat sc_template.conf.j2 
    2. server {
    3.         listen       {{ listen_port }} ;
    4.         server_name  www.sc.com;
    5.         root         /opt/dist;
    6.     access_log  /var/log/nginx/sc_access.log  main;
    7.         location / {
    8.     }
    9.     location =/api {
    10.     }
    11. }

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

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

    1. [root@scmysql opt]# cat template_test.yaml 
    2. - hosts: web
    3.   tasks:
    4.   - name: template test
    5.     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变量。

    1. [root@scmysql ansible]# cat hosts
    2. [web]
    3. 192.168.2.132:22 listen_port=81 # 指明监听的端口为81
    4. 192.168.2.137  listen_port=666
    5. [nginx]
    6. 192.168.2.11 # 随便测试的一个代码

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

    1. [root@sc-master opt]# cat test.j2
    2. #########################################################################
    3. # File Name: test.sh
    4. # Description: test.sh
    5. # Author: lkong
    6. # mail: lkong@tencent.com
    7. # Created Time: 2022-08-21 17:24:54
    8. #########################################################################
    9. #!/bin/bash
    10. hello {{ name }}
    11. shanshan shi zhu

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

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

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

    1. [root@sc-master opt]# ansible-playbook template_test.yaml -e "name=sanchuang"
    2. [WARNING]: Found variable using reserved name: name
    3. PLAY [webser] *****************************************************************************
    4. TASK [Gathering Facts] ********************************************************************
    5. ok: [192.168.2.137]
    6. ok: [192.168.2.132]
    7. TASK [text test] **************************************************************************
    8. changed: [192.168.2.137]
    9. changed: [192.168.2.132]
    10. PLAY RECAP ********************************************************************************
    11. 192.168.2.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    12. 192.168.2.137 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

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

    B机器:

    1. [root@sc-slave lianxi]# ls
    2. ansible test.sh
    3. [root@sc-slave lianxi]# cat test.sh
    4. #########################################################################
    5. # File Name: test.sh
    6. # Description: test.sh
    7. # Author: lkong
    8. # mail: lkong@tencent.com
    9. # Created Time: 2022-08-21 17:24:54
    10. #########################################################################
    11. #!/bin/bash
    12. hello sanchuang
    13. shanshan shi zhu

    C机器上:

    1. [root@nginx-kafka03 lianxi]# ls
    2. aa ansible bb cc file_num.sh lianxi test.sh tongle xieshan
    3. [root@nginx-kafka03 lianxi]# cat test.sh
    4. #########################################################################
    5. # File Name: test.sh
    6. # Description: test.sh
    7. # Author: lkong
    8. # mail: lkong@tencent.com
    9. # Created Time: 2022-08-21 17:24:54
    10. #########################################################################
    11. #!/bin/bash
    12. hello sanchuang
    13. shanshan shi zhu

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

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

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

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

    也可以生效。

  • 相关阅读:
    数据分析第十三讲:数据可视化入门(二)
    Spring Task单机定时任务(使用及阻塞问题解决)
    学生体育运动主题网页设计——兵乓球国乒网(纯HTML+CSS代码)
    lesson4-C++内存管理
    yolov5模型代码怎么修改
    JUC并发编程第四篇,Java中的各种锁之乐观锁和悲观锁、公平锁和非公平锁、可重入锁以及死锁基础
    Vue 模板语法 v-bind
    零基础自学javase黑马课程第十五天
    上海亚商投顾:三大指数小幅上涨 HBM概念股全天强势
    宝贝快出生的这三个表现,孕妈尽快去医院待产
  • 原文地址:https://blog.csdn.net/m0_52165864/article/details/126449882