shenyg 发表于 2018-7-29 06:59:04

ansible playbook lookups组件

  1.lookups file
  支持从外部数据拉取信息,例如从数据库里拉取信息,然后定义给一个变量的形式

[*]hosts: all  gather_facts: true
  vars:
  contents: "{{ lookup('file', '/etc/sysconfig/network') }}"
  tasks:

[*]name: debug lookups  debug: msg="The contents is {% for i in contents.split("\n") %} {{ i }} {% endfor %}"
  结果:
  PLAY ****

  TASK ****
  ok:
  TASK **
  **ok: => {
  "msg": "The contents is# Created by anacondaNETWORKING_IPV6=noPEERNTP=no "
  }
  PLAY RECAP *
  192.168.1.1             : ok=2    changed=0    unreachable=0    failed=0 **
  2.lookups password对传入的内容进行加密处理

[*]hosts: all  gather_facts: False
  vars:
  contents: "{{ lookup('password', 'ansible_book') }}"
  tasks:

[*]name: debug lookups  debug: msg="The contents is {{ contents }}"

  3.lookups pipe
  pipe lookups控制机上调用subprocess.Popen执行命令,然后将命令的结果传递给变量

[*]hosts: all  gather_facts: False
  vars:
  contents: "{{ lookup('pipe', 'date +%Y-%m-%d') }}"
  tasks:

[*]name: debug lookups  debug: msg="The contents is {% for i in contents.split("\n") %} {{ i }} {% endfor %}"
  结果:
  TASK
  ok: => {
  "msg": "The contents is2018-02-03 "
  }

  4.lookups redis_kv
  redis_kv就是从redis数据库中get数据,需要安装redis python库:yum install python-redis


  5.lookups template
  读取文件template.j2,里面定义的变量是每台主机的fatcs信息
  worker_processes {{ ansible_processor_cores }};
  IPaddress {{ ansible_eth0.ipv4.address }}

页: [1]
查看完整版本: ansible playbook lookups组件