Ansible Lookup

Ansible Lookup
Techiio-author
Written by Nilima PaulJanuary 27, 2022
10 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog post. We will discuss Ansible Lookup.

Introduction to Ansible Lookup

In Ansible, we have progressed highlights like query modules, which are Ansible explicit expansion to the Jinja2 templating language. Utilizing query modules we can assess information on the Ansible control machine. This information is assessed by modules locally. Yet, intriguing is the way that the wellspring of this information can be outside datastores or administrations. This component makes query modules strong and valuable.

There is a major rundown of accessible modules including record, vars, secret word, URL, and some more, which we can use according to our current circumstance, we will investigate a portion of those in this report.

What is Ansible Lookup?

In Ansible, utilizing query modules, we can get information from outside assets. These sources can be neighborhood filesystems or some outside datastores or administrations. The information got by those sources is then assessed by modules and returned utilizing Ansible templating frameworks and made accessible in that arrangement.

Ansible Lookup module is generally utilized for stacking factors/layouts containing data of or from outer frameworks.

We should note beneath focuses while working with Ansible query modules: -

  • Lookups are executed with a functioning registry comparative with the play or job, which is not the same as nearby errands which work with working index connected with the executed
  • This is a development highlight so you should have great information on Ansible, its an approach to working, and documentation source before utilizing query modules in
  • All the handling of information is done locally so we should realize that information after handling or assessing should pass into a variable or use as a wellspring of
  • Custom modules can be made initiated by one of the accompanying ways: -
  • Placing it in lookup_plugins registry neighboring
  • Placing it in lookup_plugins registry inside a
  • Placing in one of query registries sources arranged in cfg
  • We can join Ansible modules with channels and tests to control the information or create more information.
  • From Ansible v2.5, we have a new jinja2 work that was added for conjuring query modules, which is named as a question, the fundamental contrast between both is that the return upsides of the inquiry are generally a rundown while query returns comma-isolated qualities (CSV). We can utilize wantlist=True expressly with a query to make it return a

How Does Ansible Lookup Works?

Ansible has a rundown of accessible default query modules which can be utilized. Alongside these one can likewise make custom modules to make use of witofery. The accessible rundown of query modules might fluctuate from Ansible variant to form.

To get a list of available lists of lookup plugins, you can use the below command: –

ansible-doc -t lookup -l

This will output something like below, from this list you can choose a lookup plugin, and as we said this list depends on the Ansible Version you have.

blogpost

After choosing a lookup plugin, we must read its documentation and check possible examples by using the below command:

ansible-doc -t lookup <plugin name>

Like we use below, where we are checking Ansible lookup plugin dig.

ansible-doc -t lookup dig

Output:

blogpost

Though you can have different requirements of plugins in your environment, we would like to list a few common and mostly used plugins below, just to add to our knowledge, so that you can make use of same if needed: –

  • Config: This is to query the current qualities from the Ansible Configuration
  • CSV file: This is utilized to utilize information from a CSV
  • edit: This module is utilized to return esteems in key-esteem
  • burrow: This is utilized to question DNS by utilizing a library named
  • env: This is utilized to peruse the upsides of env.
  • document: This is utilized to peruse the substance of a
  • scaled-down: This module is to peruse substance from an ini
  • inventory_hostnames: To return the rundown of matching example
  • Things: To return the thing's rundown
  • lines: To peruse a line from the order
  • list: To return a similar result which is given as
  • secret key: To produce or recover an irregular
  • layout: To return substance of a record in the wake of utilizing jinja2 to format
  • URL: This is utilized to get explicit substance from a
  • vars: This is to query the factors which are either templated or pronounced in Play

Examples to Implement Ansible Lookup

Presently by utilizing models, we will attempt to find out with regards to some of Ansible query modules, which you may need to use in everyday tasks. We will take a few continuous models, however before going there, we initially comprehend our lab, which we utilized for testing reasons.

  • Here we have an Ansible control server named ansible-regulator and two controllers have named have one and host-two. We will make playbooks and run Ansible orders on the ansible-regulator hub and see the outcomes on remote hosts.
  • We will attempt to investigate a portion of the Ansible Lookup Plugins as a visual cue. The utilization in these models is totally to tell you the best way to utilize these, your genuine use might be unique about these.

Example #1

In this model, we will take substance from a nearby record on the Ansible control machine and show its result utilizing the Ansible query module For this we make a playbook, as underneath:

Code:

hosts:
localhost
tasks:
name: here we will display the contents of
/tmp/samplefile debug:
msg: These are the contents of /tmp/samplefile -
{{lookup('file', '/tmp/samplefile')}}

We get output like below:

ansible-playbook ansible_lookup_file.YAML

Output:

blogpost

Example #2

In this example, we will make a variable using lookup plugin vars. We will attempt to print hostnames of remote machines, for this, we create a playbook like below:

Code:

hosts:
all vars:
year:
hostname tasks:
name: Show the value of 'variable name'
debug:
msg: "{{ lookup('vars', 'ansible_h' + myvar)}}"

On executing we get an output like below:

ansible-playbook ansible_lookup_vars1.YAML

Output:

blogpost

Conclusion

As we saw Ansible have has modules that have convenience in assortment across numerous advances. In any case, we should deal with the language structure to get the ideal outcome. Likewise, the documentation for every query module should be alluded to before utilizing a module, as each module works unexpectedly. So learn it first then, at that point, use it.

Ansible
Linux
Ansible lookup
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance