前沿:
ansible的那幾個插件都不錯,現在咱們聊聊lookup plugins這個插件。 Lookup_plugins主要是用來實現擴展playbook裏面各種的字符串和變量的擴展。對於我個人來說,用途不是太大,但是看了下官方提供的lookup_plugins的擴展列表,還是有些想法的。
一開始使用lookup的時,遇到了一個問題,` lookup `裏面的數據沒有處理,怎麼也找不到解決的方法,最後問題在於ubuntu下的版本問題。貌似那個版本對lookup組建支持不好。
感謝沈燦的在旁邊扯淡,雖然沒啥效果,最後換系統解決了問題~
看這個例子,他的意思是說,通過lookup找到file這個插件擴展,file.py的功能主要是查看/etc/foo.txt文件的各種屬性 。
- hosts: all vars: contents: "{{ lookup('file', '/etc/foo.txt') }}" tasks: - debug: msg="the value of foo.txt is {{ contents }}"
這裏在show下更多的lookup_plugins的擴展。
pipe一看這名字,管道,執行linux命令。
redis_kv是 鏈接到redis 做操作 。
......
原文:http://rfyiamcool.blog.51cto.com/1030776/1441451
- hosts: all tasks: - debug: msg="{{ lookup('env','HOME') }} is an environment variable" - debug: msg="{{ item }} is a line from the result of this command" with_lines: - cat /etc/motd - debug: msg="{{ lookup('pipe','date') }} is the raw result of running this command" - debug: msg="{{ lookup('redis_kv', 'redis://localhost:6379,somekey') }} is value in Redis for somekey" - debug: msg="{{ lookup('dnstxt', 'example.com') }} is a DNS TXT record for example.com" - debug: msg="{{ lookup('template', './some_template.j2') }} is a value from evaluation of this template"
我們來看下pipe.py的插件實現代碼:
代碼很是乾脆,就是subprocess執行,然後return ret。
import subprocess from ansible import utils, errors class LookupModule(object): def __init__(self, basedir=None, **kwargs): self.basedir = basedir def run(self, terms, inject=None, **kwargs): terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) if isinstance(terms, basestring): terms = [ terms ] ret = [] for term in terms: term = str(term) p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) (stdout, stderr) = p.communicate() if p.returncode == 0: ret.append(stdout.decode("utf-8").rstrip()) else: raise errors.AnsibleError("lookup_plugin.pipe(%s) returned %d" % (term, p.returncode)) return ret
原文:http://rfyiamcool.blog.51cto.com/1030776/1441451
我這跑的一個測試,簡單:
通過結果我們看到,他會從lookup插件,找到相應的插件,然後來處理數據。 這個數據是本地的。
官方的redis_kv感覺有些麻煩,就自己寫了個簡單的lookup關於redis的插件。
我的執行過程,playbook裏面調用,{{ lookup('redis_kv','blog') }} 他會從redis裏面get blog這個key鍵。
[root@vm-10-154-252-46 lookup_plugins]# ansible web -a 'cat /root/b' 10.154.252.47 | success | rc=0 >> [root@vm-10-154-252-46 lookup_plugins]# ansible-playbook ~/web1.yaml -vvvvv [WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update it (ie. yum update gmp). PLAY [web] ******************************************************************** GATHERING FACTS *************************************************************** <10.154.252.47> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.154.252.47 <10.154.252.47> REMOTE_MODULE setup <10.154.252.47> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1406018982.81-198119391249257 && echo $HOME/.ansible/tmp/ansible-tmp-1406018982.81-198119391249257' <10.154.252.47> PUT /tmp/tmpXzttC_ TO /root/.ansible/tmp/ansible-tmp-1406018982.81-198119391249257/setup <10.154.252.47> EXEC /bin/sh -c 'LC_CTYPE=C LANG=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1406018982.81-198119391249257/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1406018982.81-198119391249257/ >/dev/null 2>&1' ok: [10.154.252.47] blog TASK: [lineinfile dest=/root/b regexp='nima' line="xiaorui.cc" owner=root group=root mode=0644] *** <10.154.252.47> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO 10.154.252.47 blog <10.154.252.47> REMOTE_MODULE lineinfile dest=/root/b regexp='nima' line="xiaorui.cc" owner=root group=root mode=0644 <10.154.252.47> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1406018984.36-174537627795378 && echo $HOME/.ansible/tmp/ansible-tmp-1406018984.36-174537627795378' <10.154.252.47> PUT /tmp/tmpuuRUgy TO /root/.ansible/tmp/ansible-tmp-1406018984.36-174537627795378/lineinfile <10.154.252.47> EXEC /bin/sh -c 'LC_CTYPE=C LANG=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1406018984.36-174537627795378/lineinfile; rm -rf /root/.ansible/tmp/ansible-tmp-1406018984.36-174537627795378/ >/dev/null 2>&1' changed: [10.154.252.47] => {"backup": "", "changed": true, "msg": "line added"} PLAY RECAP ******************************************************************** 10.154.252.47 : ok=2 changed=1 unreachable=0 failed=0 [root@vm-10-154-252-46 lookup_plugins]# ansible web -a 'cat /root/b' 10.154.252.47 | success | rc=0 >> xiaorui.cc [root@vm-10-154-252-46 lookup_plugins]# #原文http://rfyiamcool.blog.51cto.com/1030776/1441451
總結下,這東西 就是用來做playbook的擴展的。 我設立一個變量,但是這個變量是不固定的,需要每次推送playbook的時候,做更新的。 這個時候就可以考慮用lookup plugins插件做支持。