3 ansible-playbook 条件语句-外部变量使用
外部变量指的是从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 }}
分类:
自动化运维工具
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构