#
# Topic: Ansible lookups组件
# State:
# 1. 通过lookups组件,Ansible可以从其他地方拉取数据
# 2
# Testing environment:
# OS: RHEL6.6
# Python: python2.7.5
# Ansible: ansible 2.3.1.0
# Nodes: 3
#
-
- lookups file:
- 原理:
利用python codecs.open模块打开一个文件并返回文件内容 - 用法:
可以将返回的结果放在一个变量中,然后使用python的字符串处理方法进行处理 - 示例:
# 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:
- 原理:
对获取的内容进行加密处理 - 用法:
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:
- 原理:
相当于linux中的管道的作用, 将一个命令输出结果返回 - 用法:
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数据库, 从redis中获取数据 -
用法:
-
示例:
-
- lookups template:
-
原理:
先渲染jinja2模板之后再读取文件内容 -
用法:
-
示例:
- 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
网友评论