You are reading the article How Does Ansible Lookup Works With Examples updated in September 2023 on the website Nhunghuounewzealand.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does Ansible Lookup Works With Examples
Introduction to Ansible LookupWeb development, programming languages, Software testing & others
There is a big list of available plugins including file, vars, password, URL, and many more, which we can use as per our environment, we will explore some of those in this document.
What is Ansible Lookup?Ansible Lookup plugin is mostly used for loading variables/templates containing information of or from external systems.
We must note below points while working with Ansible lookup plugins: –
Lookups are executed with a working directory relative to the play or role, which is different from local tasks which work with working directory related to the executed
All the processing on data is done locally so we must know that data after processing or evaluating must pass into a variable or use as a source of
Custom plugins can be made activated by one of the following ways: –
Putting it in lookup_plugins directory adjacent to
Putting it in lookup_plugins directory inside a
Putting in one of lookup directories sources configured in cfg
We can combine Ansible plugins with filter and tests to manipulate the data or generate more data.
From Ansible v2.5, we have a new jinja2 function was added for invoking lookup plugins, which is named as query, the main difference between both is that the return values of the query are always a list while lookup returns comma-separated values (CSV). We can use wantlist=True explicitly with a lookup to make it return a
How Does Ansible Lookup Works?Ansible has a list of available default lookup plugins which can be used. Along with these we one can also create custom plugins to make use with lookup. The available list of lookup plugins may vary from Ansible version to version.
To get list of available lists of lookup plugins, you can use 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.
After choosing a lookup plugin, we must read its documentation and check possible examples by using below command:
Like we use below, where we are checking Ansible lookup plugin dig.
ansible-doc -t lookup dig
Output:
Though you can have different requirement of plugins in your environment, we would like to list 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 lookup the current values from the Ansible Configuration
csvfile: This is used to use data from a CSV
dict: This plugin is used to return values in key-value
dig: This is used to query DNS by using a library named
env: This is used to read the values of env.
file: This is used to read the contents of a
mini: This plugin is to read contents from an ini
inventory_hostnames: To return the list of matching pattern
Items: To return the item’s list
lines: To read a line from the command
list: To return the same output which is given as
password: To generate or retrieve a random
template: To return contents of a file after using jinja2 to template
url: This is used to get specific content from a
vars: This is to lookup the variables which are either templated or declared in Play
Examples to Implement Ansible LookupNow by using examples, we will try to learn about some of Ansible lookup plugins, which you might have to use in day to day operations. We will take some real-time examples, but before going there, we first understand our lab, we used for testing purpose.
Here we have an Ansible control server named ansible-controller and two remotes hosts named host- one and host-two. We will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.
We will try to explore some of the Ansible Lookup Plugins by example. The use in these examples is completely to show you how to use these, your real-life use may be different from these.
Example #1In this example, we will take contents from a local file on the Ansible control machine and display its output using Ansible lookup plugin For this we create a playbook, like below:
Code:
{{lookup(‘file’, ‘/tmp/samplefile’)}}
We get output like below:
ansible-playbook ansible_lookup_file.yaml
Output:
Example #2In 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:
msg: “{{ lookup(‘vars’, ‘ansible_h’ + myvar)}}”
On executing we get an output like below:
ansible-playbook ansible_lookup_vars1.yaml
Output:
ConclusionAs we saw that Ansible have lookup plugins which have usability in variety across many technologies. But we must take care of the syntax very well to get the desired result. Also, the documentation for each lookup plugin must be referred before using a plugin, as each plugin works differently. So learn it first then use it.
Recommended ArticlesThis is a guide to Ansible Lookup. Here we discuss an introduction to Ansible Lookup, syntax, how does lookup work with examples to implement. You can also go through our other related articles to learn more –
You're reading How Does Ansible Lookup Works With Examples
Update the detailed information about How Does Ansible Lookup Works With Examples on the Nhunghuounewzealand.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!