lookup,类似于取值,通过执行lookup,获取信息。
ansible-doc -t lookup -l
列出了 ansible 自带的 lookup
下面列举几个例子
---
- hosts: all
remote_user: root
gather_facts: no
vars:
users:
a: x
b: y
tasks:
- name: test01
debug:
msg: "index is {{ item.0 }}, value is {{ item.1 }}, {{ item }}"
with_indexed_items: ['a','b','c']
- name: test02
debug:
msg: "index is {{ item.0 }}, value is {{ item.1 }}, {{ item }}"
loop: "{{ lookup('indexed_items',['a','b','c']) }}"
- name: test03
debug:
msg: "{{ item.key }} is {{ item.value }}"
with_dict: "{{ users }}"
- name: test04
debug:
msg: "{{ item.key }} is {{ item.value }}"
loop: "{{ lookup('dict',users) }}"
- name: test05
debug:
msg: " {{ item }} "
with_items: " {{ lookup('file', '/root/test/20190528/test01.txt') }} "
- name: test01
debug:
msg: " {{ lookup('env', 'HOME') }}"
- name: test02
debug:
msg: " {{ item }} "
with_list:
- 1
- [2,3]
- 4
# 执行
TASK [test01] *********************************************************************************************************************************************************************************
ok: [dbw21as] => (item=[0, u'a']) => {
"msg": "index is 0, value is a, [0, u'a']"
}
ok: [dbw21as] => (item=[1, u'b']) => {
"msg": "index is 1, value is b, [1, u'b']"
}
ok: [dbw21as] => (item=[2, u'c']) => {
"msg": "index is 2, value is c, [2, u'c']"
}
TASK [test02] *********************************************************************************************************************************************************************************
ok: [dbw21as] => (item=[0, 'a']) => {
"msg": "index is 0, value is a, [0, 'a']"
}
ok: [dbw21as] => (item=[1, 'b']) => {
"msg": "index is 1, value is b, [1, 'b']"
}
ok: [dbw21as] => (item=[2, u'c']) => {
"msg": "index is 2, value is c, [2, 'c']"
}
TASK [test03] *********************************************************************************************************************************************************************************
ok: [dbw21as] => (item={'value': u'x', 'key': u'a'}) => {
"msg": "a is x"
}
ok: [dbw21as] => (item={'value': u'y', 'key': u'b'}) => {
"msg": "b is y"
}
TASK [test04] *********************************************************************************************************************************************************************************
ok: [dbw21as] => (item={'key': u'a', 'value': u'x'}) => {
"msg": "a is x"
}
ok: [dbw21as] => (item={'key': u'b', 'value': u'y'}) => {
"msg": "b is y"
}
TASK [test05] *********************************************************************************************************************************************************************************
ok: [dbw21as] => (item=a
b
c) => {
"msg": " a\nb\nc "
}
TASK [test01] **********************************************************************************
task path: /root/ansible-test/test.yml:8
ok: [dbw21as] => {
"msg": " /root"
}
TASK [test02] **********************************************************************************
task path: /root/ansible-test/test.yml:12
ok: [dbw21as] => (item=1) => {
"msg": " 1 "
}
ok: [dbw21as] => (item=[2, 3]) => {
"msg": " [2, 3] "
}
ok: [dbw21as] => (item=4) => {
"msg": " 4 "
}
======================================================
自定义lookup
vim plugins/lookup/dbops.py
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible import constants as C
from ansible.plugins import loader
from ansible.plugins.lookup import LookupBase
import psycopg2
def dbops(hostname):
result = []
sql = 'select full_hostname from db_ops_host where short_hostname = \'%s\' ' % hostname
conn = psycopg2.connect(host = 'db01.test.com', database = 'dbops', user = 'dbadmin', port = 5432)
cursor = conn.cursor()
cursor.execute(sql)
full_hostname = cursor.fetchone()[0]
result.append(full_hostname)
cursor.close()
conn.close()
return result
class LookupModule(LookupBase):
def run(self, terms, **kwargs):
res = []
for term in terms:
x = dbops(term)
res.append(x)
return res
vim test.yml
---
- hosts: all
remote_user: root
gather_facts: no
vars:
list1: [{'a':'a'}, {'b':'b'}, {'c':'c'}]
tasks:
- name: test01
debug:
msg: " {{ lookup('dbops' , 'dbw01at', 'dbw01bt') }} "
运行结果
TASK [test01] **********************************************************************************
ok: [dbw21as] => {
"msg": " [[u'dbw01at.daodao.com'], [u'dbw01bt.daodao.com']] "
}