Playbook变量及Template使用(四)
变量来源:(1)ansible setup远程主机的所有变量
(2)自定义变量
查看内置变量:
# ansible test -m setup
列出所有主机的ansible_all_ipv4_addresses变量
# ansible test -m setup -a 'filter=ansible_all_ipv4_addresses'
自定义变量:
方式一:
# tail -3 /etc/ansible/hosts
172.16.252.193 hostname=web1
172.16.253.131 hostname=web2
使用ansible调用: 调用方式{{ va }}
# ansible test -m hostname -a 'name=cce-{{ hostname}}'
方式二:对一个组单独定义一个变量组
# tail -6 /etc/ansible/hosts
172.16.252.193
172.16.253.131
hostname=last
cce=hostname
使用ansible调用: 调用方式{{ var }}
# ansible test -m hostname -a 'name={{ cce }}-{{ hostname}}'
方式三:通过命令行指定变量
# ansible test-e newtest=mysql -e nginx=web -m hostname -a 'name={{ newtest }}-{{ nginx}}'
Template语法,Jinja2语法格式
功能:根据模块文件动态生成对应的配置文件
1、Template文件必须存放目录名为template下,且命名为.j2结尾
2、yaml/yml playbook文件需和template目录平级
算数运算:
+- * / % **
比较操作符:
== >= != <= < >
逻辑运算符:
And or not
流表达式:
For、 If、 When
测试:
# mkdir ansible/
# mkdir templates/
# cd templates/
# cp /root/httpd.conf httpd.conf.j2
目录结构
# tree ansible/
ansible/
|-- temphttpd.yml
`-- templates
`-- httpd.conf.j2
使用template进行文件分发
# cat temphttpd.yml
---
- hosts: test
remote_user: root
tasks:
- name: template config
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
使用变量来控制template:
使用内置变量:
# ansible test -m setup |grep ansible_processor_vcpus
"ansible_processor_vcpus": 1,
"ansible_processor_vcpus": 1,
嵌入变量
# grep "^Listen" templates/httpd.conf.j2
Listen {{ ansible_processor_vcpus }}
测试
方法二:
# cat templates/httpd.conf.j2 |grep "^Listen"
Listen {{ port }}
流表达式
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen | default('80 default_server') }};
{% if vhost.server_name is defined %}
server_name {{ vhost.server_name }};
{% endif %}
{% if vhost.root is defined %}
root {{ vhost.root }};
{% endif %}
使用for来对nginx.conf的监听地址进行修改
1、首先定义template
# pwd
/root/ansible
# cat templates/nginx.conf.j2
{% for vhost in nginx_vhosts %}##此处和shell脚本差不多表示for vhost in nginx_vhosts
server{
Listen {{ vhost.listen }}##此处表示上面的vhost循环里面的listen指
}
{% endfor %}##结束符
2、定义yml,和变量
# cat nginx.yml
---
- hosts: test
remote_user: root
vars:##定义变量
nginx_vhosts:##定义循环体
- listen: 8080##第一个listen为8080
tasks:
- name: templates nginx
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
测试执行
结果
注意:如果nginx定义成了如下,那么将会循环两次
# cat nginx.yml
---
- hosts: test
remote_user: root
vars:
nginx_vhosts:
- listen: 8080
- listen: 80
tasks:
- name: templates nginx
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
结果
循环变量:
# cat nginx.yml
---
- hosts: test
remote_user: root
vars:
nginx_vhosts:
- web1: ##第一个变量其中有三个值(多变量只能这么写)
listen: 8080
root: "/var/www/nginx"
server_name: "web1"
- web2: ##第二个变量其中有三个值(多变量只能这么写)
listen: 8080
root: "/var/www/nginx"
server_name: "web1"
tasks:
- name: templates nginx
template: src=nginx.conf dest=/etc/nginx/nginx.conf
结果
if判断:
编写yml实现让版本为7的机器来安装vsftpd
---
- hosts: test
remote_user: root
tasks:
- name: Install vsftpd
yum: name=vsftpd state=present
when: ansible_distribution_major_version == "7"
编写yml实现让ip为172.16.0.6的机器来启动vsftpd
# cat vsftpd.yml
---
- hosts: test
remote_user: root
tasks:
- name: Install vsftpd
yum: name=vsftpd state=present
when: ansible_distribution_major_version == "7"
- name: service start vsftpd
service: name=vsftpd state=started
when: ansible_all_ipv4_addresses == "172.16.0.6"
playbook迭代with_items,假如我们一次性要安装或者复制多个文件时可以使用此参数
1、一次性安装多个软件包
# cat it.yml
---
- hosts: test
remote_user: root
tasks:
- name: Install lrzsz
yum: name={{ item }} state=present
with_items:
- lrzsz
- httpd
2、一次性复制多个文件
# cat copy.yml
---
- hosts: test
remote_user: root
tasks:
- name: copy files
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
- a
- b
页:
[1]