Technology Security Analyst
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.
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: -
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.
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
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: –
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.
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
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
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.