#
# Topic: Ansible lookups組件
# State:
# 1. 通過lookups組件,Ansible可以從其他地方拉取數(shù)據(jù)
# 2
# Testing environment:
# OS: RHEL6.6
# Python: python2.7.5
# Ansible: ansible 2.3.1.0
# Nodes: 3
#
-
- lookups file:
- 原理:
利用python codecs.open模塊打開一個(gè)文件并返回文件內(nèi)容 - 用法:
可以將返回的結(jié)果放在一個(gè)變量中,然后使用python的字符串處理方法進(jìn)行處理 - 示例:
# lookups file
-
name: lookups file
remote_user: root
hosts: tomcat
gather_facts: no
tasks:- name: lookups file
vars:
user: "{{ lookup('file', '/etc/ansible/looksup/looks.yml') }}"
debug: msg="{% for i in user.split("\n") %}{{i}}{% endfor %}"
- name: lookups file
-
- lookups password:
- 原理:
對獲取的內(nèi)容進(jìn)行加密處理 - 用法:
lookup('password', 'file_path') - 示例:
# lookups file
-
name: lookups file
remote_user: root
hosts: tomcat
gather_facts: no
tasks:- name: lookups file
vars:
user: "{{ lookup('password', '/etc/ansible/looksup/looks.yml') }}"
debug: msg="{{user}}"
- name: lookups file
-
- lookups pipe:
- 原理:
相當(dāng)于linux中的管道的作用, 將一個(gè)命令輸出結(jié)果返回 - 用法:
lookup('pipe', 'command') - 示例:
-
hosts: tomcat
gather_facts: no
tasks:- name: the first task
vars:
content: "{{ lookup('pipe', 'date +%y-%m-%d') }}"
debug: msg="{% for i in content.split("\n") %} {{ i }} {% endfor %}"
- name: the first task
-
- lookups redis_kv:
原理:
鏈接redis數(shù)據(jù)庫, 從redis中獲取數(shù)據(jù)用法:
示例:
-
- lookups template:
原理:
先渲染jinja2模板之后再讀取文件內(nèi)容用法:
示例:
- hosts: tomcat
remote_user: root
tasks:- name: first
vars:
content: "{{lookup('template', './tmp.j2')}}"
debug: msg="{% for i in content.split("\n") %} {{i}} {% endfor %}"
- name: first