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 [all] ****
- name: debug lookups
TASK [Gathering Facts] ****
ok: [192.168.1.1]
TASK [debug lookups] **
**ok: [192.168.1.1] => {
"msg": "The contents is # Created by anaconda NETWORKING_IPV6=no PEERNTP=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 }}"
- name: debug lookups
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 [debug lookups]
ok: [47.100.16.187] => {
"msg": "The contents is 2018-02-03 "
}
- name: debug lookups
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 }}
转载于:https://blog.51cto.com/yangxiongchun/2068446