ansible-playbook 04 模板when用法
2020-07-12 本文已影响0人
xlgao
ansible-playbook模板的使用-when
需求
系统版本不同,根据系统版本使用对应的nginx模板
具体实现
- 不知系统版本变量名称的情况下搜索变量名
[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
即为系统版本.
- 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
}
- 编写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
- 准备不同系统版本的nginx模板文件
[root@localhost playbook]# ls templates/
nginx.conf6.j2 nginx.conf7.j2
- 修改模板文件内容,详见
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;
}
}
- 执行
[root@localhost playbook]# ansible-playbook nginx.yaml