首页 技术 正文
技术 2022年11月19日
0 收藏 384 点赞 3,566 浏览 2309 个字

外部变量指的是从playbook文件之外获取的数值

lookups file

file是我们经常使用的一种lookups的方式,它的原理就是使用python的codecs.open打开文件然后把结果返回给变量,请看例子

[root@LeoDevops playb]# cat lookups_file.yaml
- hosts: u12
gather_facts: False
vars:
contents: "{{ lookup('file','/etc/sysconfig/network') }}"
tasks:
- name: debug file lookup
debug: msg="the content is {% for i in contents.split('\n') %} ,{{ i }} {% endfor %} "

执行结果如下:

[root@LeoDevops playb]# ansible-playbook lookups_file.yaml PLAY [u12] ************************************************************************************************************************************************************************************TASK [debug file lookup] **********************************************************************************************************************************************************************
ok: [192.168.93.137] => {
"msg": "the content is ,NETWORKING=yes ,HOSTNAME=LeoDevops "
}PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.137 : ok=1 changed=0 unreachable=0 failed=0

lookup password

对传入的内容加密处理

[root@LeoDevops playb]# cat lookup_passwd.yaml
- hosts: u12
gather_facts: False
vars:
contents: "{{ lookup('password','ansible_book') }}"
tasks:
- name: debug lookups
debug: msg="true contents is {{ contents }}"

lookups pipe

pipe lookups的实现原理很简单,其实调用的是subprocess.Popen执行的

[root@LeoDevops playb]# cat  lookup_pipe.yaml
- hosts: u12
gather_facts: False
vars:
contents: "{{ lookup('pipe','date +%F') }}"
tasks:
- name: debug lookups
debug: msg="the content is {% for i in contents.split('\n') %} -- {{ i }} {% endfor %}"

lookups redis_kv

redis_kv是从redis数据库中get数据,依赖于python的redis库,

[root@LeoDevops playb]# cat redis_k.yaml
- hosts: u12
gather_facts: False
vars:
contents: "{{ lookup('redis_kv','redis://localhost:6379,resource_usage_rate') }}"
tasks:
- name: get_redis_k
debug: msg="the content is {% for i in contents.split('\n') %} --->{{ i }}{% endfor %}"

lookups template

template 跟file方式有点类似,都是读取文件,但是template在读取文件之前需要把jinja模板渲染完成后再读取,

[root@LeoDevops playb]# cat lookup_template.yaml
- hosts: u12
gather_facts: True
vars:
contents: "{ lookup('template','./hehe.conf' }"
tasks:
- name: debug lookup
#debug: msg="teht contents is {% for i in contents.split('\n') %} -->{{ i }} {% endfor %}"
debug: msg="teht contents is {{ contents }}" [root@LeoDevops playb]# cat hehe.conf
work_process: {{ ansible_processor_cores }}
ip_addr: {{ ansible_default_ipv4.address }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894