美文网首页
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用法

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