设为首页 收藏本站
查看: 1318|回复: 0

[经验分享] Playbook变量及Template使用(四)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-12-30 09:47:10 | 显示全部楼层 |阅读模式
变量来源:
(1)ansible setup远程主机的所有变量
(2)自定义变量
查看内置变量:
[iyunv@centos6-1 ~]# ansible test -m setup
列出所有主机的ansible_all_ipv4_addresses变量
[iyunv@centos6-1 ~]# ansible test -m setup -a 'filter=ansible_all_ipv4_addresses'
自定义变量:
方式一:
[iyunv@centos6-1 ~]# tail -3 /etc/ansible/hosts
[test]
172.16.252.193 hostname=web1
172.16.253.131 hostname=web2
使用ansible调用: 调用方式{{ va }}
[iyunv@centos6-1 ~]# ansible test -m hostname -a 'name=cce-{{ hostname  }}'
QQ截图20161230094258.jpg
方式二:对一个组单独定义一个变量组
[iyunv@centos6-1 ~]# tail -6 /etc/ansible/hosts
[test]
172.16.252.193
172.16.253.131
[test:vars]
hostname=last
cce=hostname
使用ansible调用: 调用方式{{ var }}
[iyunv@centos6-1 ~]# ansible test -m hostname -a 'name={{ cce }}-{{ hostname  }}'
QQ截图20161230094313.jpg
方式三:通过命令行指定变量
[iyunv@centos6-1 ~]# ansible test  -e newtest=mysql -e nginx=web -m hostname -a 'name={{ newtest }}-{{ nginx  }}'
QQ截图20161230094318.jpg
Template语法,Jinja2语法格式
功能:根据模块文件动态生成对应的配置文件
1、Template文件必须存放目录名为template下,且命名为.j2结尾
2、yaml/yml playbook文件需和template目录平级
算数运算:
    +  -   *   /   %   **
比较操作符:
==    >=    !=   <=   <   >
逻辑运算符:
    And or not
流表达式:
For  、 If  、 When
测试:
[iyunv@centos6-1 ~]# mkdir ansible/
[iyunv@centos6-1 ansible]# mkdir templates/
[iyunv@centos6-1 ansible]# cd templates/
[iyunv@centos6-1 templates]# cp /root/httpd.conf httpd.conf.j2
目录结构
[iyunv@centos6-1 ~]# tree ansible/
ansible/
|-- temphttpd.yml
`-- templates
`-- httpd.conf.j2
使用template进行文件分发
[iyunv@centos6-1 ansible]# cat temphttpd.yml
---
- hosts: test
remote_user: root
tasks:
- name: template config
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
QQ截图20161230094501.jpg
使用变量来控制template:
使用内置变量:
[iyunv@centos6-1 ansible]# ansible test -m setup |grep ansible_processor_vcpus
"ansible_processor_vcpus": 1,
"ansible_processor_vcpus": 1,
嵌入变量
[iyunv@centos6-1 ansible]# grep "^Listen" templates/httpd.conf.j2
Listen {{ ansible_processor_vcpus }}
测试
QQ截图20161230094507.jpg

方法二:
[iyunv@centos6-1 ansible]# cat templates/httpd.conf.j2 |grep "^Listen"
Listen {{ port }}
QQ截图20161230094512.jpg
流表达式
{% 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
[iyunv@centos6-1 ansible]# pwd
/root/ansible
[iyunv@centos6-1 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,和变量
[iyunv@centos6-1 ansible]# 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
测试执行
QQ截图20161230094523.jpg
结果
QQ截图20161230094528.jpg
注意:如果nginx定义成了如下,那么将会循环两次
[iyunv@centos6-1 ansible]# 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
结果
QQ截图20161230094532.jpg
循环变量:
[iyunv@centos6-1 ansible]# 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
结果
QQ截图20161230094537.jpg
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
[iyunv@centos6-1 ansible]# 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[0] == "172.16.0.6"
QQ截图20161230094542.jpg
playbook迭代with_items,假如我们一次性要安装或者复制多个文件时可以使用此参数
1、一次性安装多个软件包
[iyunv@centos6-1 ansible]# cat it.yml
---
- hosts: test
remote_user: root
tasks:
- name: Install lrzsz
yum: name={{ item }} state=present
with_items:
- lrzsz
- httpd
2、一次性复制多个文件
[iyunv@centos6-1 ansible]# cat copy.yml
---
- hosts: test
remote_user: root
tasks:
- name: copy files
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
- a
- b


QQ截图20161230094516.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-321429-1-1.html 上篇帖子: Ansible-playbook基本使用(三) 下篇帖子: Ansible的roles功能(五) Playbook
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表