• ansible 基本使用之3 palybook


    概述

    playbook 寓意剧本,在以yml 文件格式的文件中书写相关命令编写而成,让ansible 安装剧本执行特定的操作。执行一些复杂的操作或编排的时候,ansible 命令行无法满足此时就用到了playbook。而且这些剧本还可以保存下来重复使用。

    很多应用示例可以参照ansible 中文官方文档。

    使用示例

    - hosts: webservers                    #主机组名称
      gather_facts: false                   #禁用收集远程主机信息功能,提高效率
      vars:                           #变量
        http_port: 80
        max_clients: 200
      remote_user: root                    #远程登录用户
    
      tasks:                            #任务
      - name: ensure apache is at the latest version    #每个模块执行的指令命名
        yum: 
         pkg: httpd                      #相当于命令行模式的-a "pkg=httpd state=latest"
        state: latest
      tags: install
    
      - name: write the apache config file
        template: src=/srv/httpd.j2 dest=/etc/httpd.conf
        notify:
        - restart apache
       tags: config                      #tags是把此模块命令打个标签,在执行此playbook的时候通过标签跳过某个操作或指定执行某个操作
    
      - name: ensure apache is running
        service: name=httpd state=started
       tags: restart
       register: result                   #注册变量,把此模块操作结果保存在变量result中,但是此值不是简单的命令输出结果,而是一个json数据
       debug: msg={{result}}            #调试功能,msg 固定key,会输出变量result 信息。
    
      handlers:                         #handlers中定义的操作是供前面task中某个操作通过notify 调用的,例如配置文件更新后重启nginx 操作
        - name: restart apache
          service: name=httpd state=restarted
    View Code

    ansible-playbook 使用

    ansible-playbook  nginx.yml   --syntax-check      #执行前检查语法
    ansible-playbook  nginx.yml                  #全部执行
    ansible-playbook  nginx.yml  --skip-tags  "install,restart"  #跳过引号中的标签
    ansible-playbook  nginx.yml  --tags  "install"    #只执行标签中的模块指令
    ansible-playbook  nginx.yml  --list-tags
    ansible-playbook  nginx.yml  --list-tasks
    ansible-playbook  nginx.yml  -e  name=kobe      #定义变量在playbook 直接引用即可
    View Code

    playbook的复用

    include 与import的使用

     include 的使用

    include 是以task 为单位引入的,每个task 单独一个yml 文件统一引入到一个汇总文件中

    [root@k8s-master-3 include]# cat lnmp.yml 
    ---
    - hosts: node
      gather_facts: false
      
      tasks:
      - include_tasks: nginx.yml
      - include_tasks: mysql.yml
      - include_tasks: php.yml
    [root@k8s-master-3 include]# cat nginx.yml 
    --- 
    - name: task1 
      debug: msg="install nginx" 
    
    [root@k8s-master-3 include]# cat mysql.yml 
    --- 
    - name: task2 
      debug: msg="install mysql"
    
    [root@k8s-master-3 include]# cat php.yml 
    ---
    - name: task2
      debug: msg="install php"
    
    
    ansible-playbook lnmp.yml
    View Code

    import 的使用

    import 是基于playbook 去引用的,多个playbook 的yml 文件统一引入到一个文件中去。

    cat main.yml
    ---
    - import_playbook: nginx.yml
    - import_playbook: mysql.yml
    - import_playbook: php.yml
    
    [root@k8s-master-3 include]# cat nginx.yml 
    --- 
    - hosts: webserver
      tasks:
      - name: task1 
        debug: msg="install nginx" 
    
    [root@k8s-master-3 include]# cat mysql.yml 
    --- 
    - hosts: webserver
      tasks:
     - name: task2 
        debug: msg="install mysql"
    
    [root@k8s-master-3 include]# cat php.yml 
    ---
    - hosts: webserver
      tasks:
      - name: task2
        debug: msg="install php"
    View Code

    流程控制

    条件判断when  

    判断符合条件时再执行该条件所在的模块指令

    tasks:
      - name: "shutdown Debian flavored systems"
        command: /sbin/shutdown -t now
        when: ansible_os_family == "Debian"

    实际上when 有很多使用方式,不止判断收集的远程主机信息,也可判断变量等等,还可以多个条件and or 判断。

    循环 whit_xxx

    循环一个对象,这个对象可以使字典,列表,文件,哈希等等,依次取出对象中的元素执行模块指令,模块指令中一般都会使用该元素执行相应操作

    - name: add several users
      user: name={{ item }} state=present groups=wheel
      with_items:
         - testuser1
         - testuser2
    当需要在远程主机创建多个用户的时候,就可以把所有的用户放在列表中通过循环指令来依次取出模块指令中创建即可,item 表示每次取出的元素。

    如果你在变量文件中或者 ‘vars’ 区域定义了一组YAML列表,你也可以这样做:

    with_items: "{{somelist}}"

    循环对象为字典组成的列表时

    - name: add several users
      user: name={{ item.name }} state=present groups={{ item.groups }}
      with_items:
        - { name: 'testuser1', groups: 'wheel' }
        - { name: 'testuser2', groups: 'root' }

    通过 jinja2模板渲染

    很多时候需要复制配置文件到远程主机,此时就会在本地有一个模板文件,但是不同主机配置文件中配置不一样,例如nginx.conf 不同主机里面的server_name、 端口、域名,upstream 、日志文件名等等都不一样,此时就需要把这些配置保存在变量或者文件清单中,模板文件使用jinja模板,因为通过jinja 语法使用流程控制,字典列表等功能来引入变量值。

    cat   site.j2

  • 相关阅读:
    Ajax中onreadystatechange函数不执行,是因为放在open()后
    js调用ajax案例2,使用ok
    js调用ajax案例
    通过设置ie的通过跨域访问数据源,来访问本地服务
    Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较以及问题
    Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较
    windows 系统纯净版官网下载地址
    iOS:给Git仓库上传代码时,超过100M会被拒绝(例如github和oschina)
    iOS:Xcode8以下真机测试iOS10.0和iOS10.1配置包
    iOS:高德地图的使用
  • 原文地址:https://www.cnblogs.com/fanggege/p/13284012.html
Copyright © 2020-2023  润新知