How to extend playbook with the lookup_plugins plug-in ansible O & M tool

Source: Internet
Author: User
Tags extend gmp mkdir redis

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.

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.

The code is as follows: Copy code
-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.

The code is as follows: Copy code
-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 ('pipele', '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.

The code is as follows: Copy code
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



This is a simple test:





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.





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.

The code is as follows: Copy code
[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 shoshould 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 = '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.47
Blog
<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
 
[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] #




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.

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.