ansible的lookup插件

ansible的lookup插件可以用来从外部数据读取信息,然后付给一个变量。获取外部数据信息的种类包括:读取文件内容、随机生成password、执行shell命令、读取redis键值等等。

注意,lookup的所有运算都是在ansible中控机上完成的,而不是在远程目标机上。

---
- hosts: test_server
  remote_user: root
  tasks:
  - name: 获取普通文件内容(文件是存在于ansible中控机上的)
    debug: msg="The contests is {{ lookup('file', '/tmp/foo.txt') }}"

  - name: 获取users.ini文件中,[production]块下的user字段的内容
    debug: msg="The contests is {{ lookup('ini', 'user section=production  file=users.ini') }}"

  - name: 获取env环境变量 
    debug: msg="PATH env is | {{ lookup('env', 'PATH')}}"

  - name: 执行shell命令
    debug: msg="Exec hostname cmd | {{ lookup('pipe', 'hostname') }}"

  - name: 随机生成password。默认为20位,也可指定,如length=15
    debug: msg="PASSWORD is {{ lookup('password', '/tmp/test_lookup_password_'+'{{inventory_hostname}}') }}"

  - name: 查询redis键值,需要安装Python redis包
    debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey"

  - name: 查询etcd的键值
    debug: msg="{{ lookup('etcd', 'foo') }} is a value from a locally running etcd"

  - name: 获取template
    debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template"

出处:https://blog.csdn.net/felix_yujing/article/details/79386956

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值