Ansible custom lookup_plugins plug-in for playbook Extension

Source: Internet
Author: User


Frontier:

The plug-ins of ansible are good. Now let's talk about the plug-in lookup plugins. Lookup_plugins is mainly used to expand various strings and variables in playbook. For me personally, the usage is not too big, but I still have some ideas after reading the extended list of lookup_plugins officially provided.


When I started using lookup, I encountered a problem. The data in {lookup} was not processed, so I couldn't find a solution. The final problem was the version of ubuntu. It seems that this version does not support lookup.


Thanks to Shen can, although there is no effect, the system has finally solved the problem ~


Looking at this example, it means that the extension file extension is found through lookup. The file. py function is mainly used to view various attributes of the/etc/foo.txt file.

- hosts: all  vars:     contents: "{{ lookup(‘file‘, ‘/etc/foo.txt‘) }}"  tasks:     - debug: msg="the value of foo.txt is {{ contents }}"


Here we will show more extended lookup_plugins.


PIPE Looks at this name, pipeline, and execute the Linux Command.

Redis_kv is linked to redis for operations.

......

Original article: 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"


Let's take a look at the pipe. py plug-in implementation code:

The code is simple, that is, subprocess execution, and return ret.

import subprocessfrom ansible import utils, errorsclass 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

Original article: http://rfyiamcool.blog.51cto.com/1030776/1441451

This is a simple test:


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/40/09/wKiom1POHHSQzBgDAAEMR3VpXNk358.jpg "Title =" u_2.jpg "alt =" wkiom1pohhsqzbgdaaemr3vpxnk358.jpg "/>


From the results, we can see that he will find the corresponding plug-in from the lookup plug-in and then process the data. This data is local.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/40/09/wKiom1POHGvgpQjrAAn1y9XKb-o616.jpg "Title =" aa.jpg "alt =" wKiom1POHGvgpQjrAAn1y9XKb-o616.jpg "/>


The official redis_kv is a little troublesome, so I wrote a simple lookup plug-in about redis.


My execution process is called in playbook. {Lookup ('redis _ kv', 'blog ')} he will get the key of the blog from redis.


[[Email protected] lookup_plugins] # ansible web-a 'cat/root/B '10. 154.252.47 | Success | rc = 0> [[email protected] lookup_plugins] # ansible-playbook ~ /Web1.yaml-vvvvv [Warning]: The version of GMP you have installed has a known issue regardingtiming vulnerabilities when used with pycrypto. if possible, you shoshould updateit (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] blogtask: [lineinfile DEST =/root/B Regexp = 'nim' 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.47blog <10.154.252.47> remote_module lineinfile DEST =/root/B Regexp = 'nim' 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 [[email protected] lookup_plugins] # ansible web-a 'cat/root/B '10. 154.252.47 | Success | rc = 0> xiaorui. CC [[email protected] lookup_plugins] # original http://rfyiamcool.blog.51cto.com/1030776/1441451


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/40/0C/wKiom1POJYOBIeCKAAIGkGJe4ik158.jpg "Title =" redis1.jpg "alt =" wkiom1pojyobieckaaigkgje4ik158.jpg "/>


In summary, this is used to expand the playbook. I set up a variable, but this variable is not fixed and needs to be updated every time I push the playbook. In this case, you can use the lookup plugins plug-in for support.


This article is from "Fengyun, it's her ." Blog, declined to reprint!

Ansible custom lookup_plugins plug-in for playbook Extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.