美文网首页
ansible-playbook 04 模板when用法

ansible-playbook 04 模板when用法

作者: xlgao | 来源:发表于2020-07-12 22:31 被阅读0次

ansible-playbook模板的使用-when

需求

系统版本不同,根据系统版本使用对应的nginx模板

具体实现

  1. 不知系统版本变量名称的情况下搜索变量名
[root@localhost ansible]# ansible all -m setup | grep version
       "ansible_bios_version": "VirtualBox",
       "ansible_distribution_major_version": "6",
       "ansible_distribution_version": "6.10",
       "ansible_kernel_version": "#1 SMP Mon Apr 27 15:30:33 UTC 2020",
       "ansible_product_version": "1.2",
           "version": {
           "version_info": [
       "ansible_python_version": "2.6.6",
       "ansible_bios_version": "VirtualBox",
       "ansible_distribution_major_version": "7",
       "ansible_distribution_version": "7.2",
       "ansible_kernel_version": "#1 SMP Mon Jan 25 22:07:14 UTC 2016",
       "ansible_product_version": "1.2",
           "version": {
           "version_info": [
       "ansible_python_version": "2.7.5",
           "version": "1.4.0"
               "version": "VirtualBox"
       "facter_facterversion": "3.1.4",
       "facter_kernelmajversion": "3.10",
       "facter_kernelversion": "3.10.0",
       "facter_puppetversion": "4.3.2",
           "version": "2.1.8"
       "ansible_bios_version": "VirtualBox",
       "ansible_distribution_major_version": "7",
       "ansible_distribution_version": "7.2",
       "ansible_kernel_version": "#1 SMP Mon Jan 25 22:07:14 UTC 2016",
       "ansible_product_version": "1.2",
           "version": {
           "version_info": [
       "ansible_python_version": "2.7.5",
           "version": "1.4.0"
               "version": "VirtualBox"
       "facter_facterversion": "3.1.4",
       "facter_kernelmajversion": "3.10",
       "facter_kernelversion": "3.10.0",
       "facter_puppetversion": "4.3.2",
           "version": "2.1.8"

通过分析可以看出ansible_distribution_major_version即为系统版本.

  1. setup模块的filter过滤
[root@localhost playbook]# ansible all -m setup -a 'filter=ansible_distribution_major_version'
192.168.0.109 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "6",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
192.168.0.107 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "7",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
192.168.0.108 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "7",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

  1. 编写nginx.yaml文件,添加when条件,具体如下:

[root@localhost playbook]# cat nginx.yaml
---
- hosts: all
  remote_user: root
  vars:
    - pkg1: nginx

  tasks:
    - name: install {{ pkg1 }}
      yum: name=nginx
    - name: copy template for centos6
      template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
      notify: restartsrv
    - name: copy template for centos7
      template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
      notify: restartsrv
    - name: start {{ pkg1 }} server
      service: name=nginx state=started enabled=yes

  handlers:
    - name: restartsrv
      service: name=nginx state=restarted
  1. 准备不同系统版本的nginx模板文件
[root@localhost playbook]# ls templates/
nginx.conf6.j2  nginx.conf7.j2
  1. 修改模板文件内容,详见worker_processes参数配置:
[root@localhost playbook]# cat templates/nginx.conf6.j2
user nginx;
worker_processes {{  ansible_processor_vcpus+4 }};
[root@localhost playbook]# cat templates/nginx.conf7.j2
user daemon;
worker_processes {{ ansible_processor_vcpus*2 }};

    server {
        listen       {{ port }}  default_server;
        listen       [::]:{{ port }} default_server;
    }
}

  1. 执行
[root@localhost playbook]# ansible-playbook nginx.yaml

相关文章

  • ansible-playbook 04 模板when用法

    ansible-playbook模板的使用-when 需求 系统版本不同,根据系统版本使用对应的nginx模板 具...

  • go常用包——template

    内容 1 模板基本用法2 模板进阶用法--定义变量 --条件判断 --循环 -- with语句3 模板高级用法--...

  • ansible-playbook 03 模板的使用

    ansible-playbook模板的简单应用 需求: 多台机器根据硬件指标来设置nginx的worker_pro...

  • mysql 流程控制语句 笔记!

    一般查询用法: 批量更新用法: case when: case null when 表达式 then 执行...

  • Ansible-playbook 条件判断when、pause(

    有一些模块,例如copy这个模块有一些机制能跳过本次模块的运行.其实我们也可以使用自己的条件语句去配置跳过模块,这...

  • CASE WHEN 用法

    一、用法 Case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略。Case when 相当于一个自...

  • CASE WHEN 用法

    Case具有两种格式。简单Case函数和Case搜索函数。 --简单Case函数CASEsexWHEN'1'THE...

  • case when then用法

    示例1、

  • case when用法

    case有以下几种搭配:1.in any case表示无论如何;2.in no case表示绝不。when有以下几...

  • js模板引擎

    模板的用法 引入模板 创建模板(原生语法) <% for(var i = 0; i< headers.length...

网友评论

      本文标题:ansible-playbook 04 模板when用法

      本文链接:https://www.haomeiwen.com/subject/gwbgcktx.html