How to Integrate External Data with the Ansible Lookup

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Ansible is a configuration management and provisioning tool that was created by Michael DeHaan. It can be used for various tasks in the IT industry, but its main use case is managing remote machines. Integrating external data with Ansible Lookup to enable better knowledge discovery will help organizations deploy more smoothly and efficiently.

Ansible is an open-source software tool that uses SSH to connect with remote servers and execute commands, such as adding a user or installing packages. Ansible Lookup makes it easy for IT administrators to integrate external data sources into the inventory of their infrastructure, including databases, network devices, cloud services, social media accounts and more. With this integration they can leverage automated responses from these systems when queried by Ansible using APIs or programs in order to reduce management overhead while improving reliability. The best part: you don’t need anything other than your web browser!

The “lookup ansible example” is a great way to integrate external data with the Ansible lookup. This tool can be used in many different ways, but it is most commonly used as a method of connecting to an external database.

How to Integrate External Data with the Ansible Lookup

Have you ever wondered how to access data from external sources such as files, databases, key/value stores, APIs, and other services when executing Ansible playbooks? If so, investigating Ansible lookup will be worthwhile.

This tutorial will teach you all you need to know about Ansible lookups and how to use them to obtain external data.

Continue reading to learn how to integrate data.

Prerequisites

This tutorial includes detailed instructions. If you want to join in, make sure you have the following items:

Ansible Installation Guide (Ubuntu, RHEL, CentOS, macOS)

  • To test the Tomcat installation, you’ll need a remote Linux PC. The remote node in this tutorial is Ubuntu 20.04.3 LTS.
  • One or more hosts set to perform Ansible commands and playbooks, as well as an inventory file. Myserver is the name of the remote Linux machine, and web is the inventory group for this lesson.
  • On the Ansible controller host, NGINX is installed.
  • Both your Ansible controller host and the remote node system must have Python v3.6 or later installed – this article utilizes Python v3.9 on an Ubuntu machine.

How Do You Install Python 3.6? Related:

Using Ansible lookup to find a file

You don’t usually open a file and expect everything to go well. What if Ansible is unable to locate or locates the file? To govern how errors should handle Ansible tasks, use the Ansible lookup module in the Ansible debug module.

When you run an Ansible playbook, the debug module outputs the statements. To summarize, the module enables you to debug variables or expressions without having to stop the script.

1. SSH onto your Ansible controller host as a user who manages Ansible often.

2. Next, under your home directory, create an ansible lookup playbook demo directory. The playbook you’ll use later in the section will be in this directory.

/ansible lookup playbook demo/mkdir ansible lookup playbook demo/cd

3. In the /ansible become playbook demo directory, create a file named main.yml and copy/paste the text of the following YAML playbook.

—- – Ansible’s name Demo hosts for lookup functionality using Ansible debug module: # Using remote user as ubuntu remote user: ubuntu tasks: – name: Task ignores the error if shanky.txt does not exist ansible.builtin.debug: msg: “lookup(‘file’, ‘/shanky.txt’, errors=’ignore’) msg: “lookup(‘file’, ‘/shanky.txt’, errors=’ignore’) msg: “lookup(‘file’, ‘ name: Task fails if file does not exist ansible.builtin.debug: msg: “lookup(‘file’, ‘/shanky.txt’, errors=’warn’)” builtin.debug: “lookup(‘file’, ‘/shanky.txt’, errors=’strict’)”

  1. Finally, use the command below to start the playbook (main.yml). The tasks in the playbook are subsequently executed by Ansible.

Validating the Ansible playbook using the ansible-playbook command’s —check option is a recommended practice before running the playbook. The —check parameter instructs Ansible to simulate the playbook without actually executing it.

main.yml for ansible-playbook

You can see that all of the TASKs have an OK status, indicating that they were not needed to be completed. Because Ansible couldn’t discover the shanky.txt file, each job failed to run, but no changes were necessary.

Putting the Ansible Playbook into Action (main.yml)Putting the Ansible Playbook into Action (main.yml)

Variables in the Environment

You learnt how to interact with files using Ansible lookup in the previous section. However, there are occasions when you need to read the value of environment variables that are then utilized in URL querying or posting as the header or data in the URL.

In the */*ansible become playbook demo directory, create a file named main2.yml and paste the contents of the following YAML playbook into it.

The Ansible playbook below does two tasks:

  • The first job looks for and outputs the path of the HOME environment variable on your Ubuntu computer.
  • The second job, similarly, searches for the USR environment variable but shows the no-user-found default value since the variable does not exist.

# Executing the tasks with remote user as ubuntu remote user: ubuntu tasks: —- – name: Ansible Lookup using Ansible env module demo hosts: web – name: ansible.builtin.debug: msg: “‘ lookup(‘env’, ‘HOME’)’ is the HOME environment variable.” – name: USR ansible.builtin.debug: Checking for environment variable # If the USR variable is not discovered, the default value of no-user-found is used. “‘ lookup(‘env’, ‘USR’) | default(‘no-user-found’, True)’ is the user,” says the message.

When the playbook has finished running, the first job will show the environment path. However, Ansible is unable to locate the USR user in the second task, and hence shows the no-user-found warning.

Fetching Environment Variables with an Ansible PlaybookFetching Environment Variables with an Ansible Playbook

Content Analysis of Websites

Using Ansible lookup and the built-in debug module, you already learnt how to get data from environment variables saved on your Ubuntu computer.

However, did you know that Ansible lookup is compatible with a variety of other Ansible modules? Ansible modules such as ansible.builtin.config, ansible.builtin.template, and ansible.builtin.uri are among them.

Run the ansible-doc -l -t lookup command to see all the modules that work with Ansible lookup.

In this example, you’ll interact with the Ansible URL module (ansible.builtin.uri), one of the most popular Ansible modules, to see how it works with Ansible lookup. The Ansible URL module returns the content of a requested URL for use in an Ansible playbook.

In the */*ansible become playbook demo directory, create a file named main3.yml and paste in the following YAML playbook contents.

The ansible-playbook below comprises tasks for retrieving data from a local HTML page and a website.

— – name: Ansible Lookup functionality with Ansible URL module demo # Executing the tasks on the inventory group (web) hosts: web # Executing the tasks with remote_user as ubuntu remote_user: ubuntu tasks: – name: Task sends the request to index.html page on nginx installed locally to retrive the data ansible.builtin.debug: msg: “{{ lookup(‘url’, ‘<http://localhost:80/index.html>’)}}” – name: Task sends the request to adamtheautomator website and retrive the data ansible.builtin.debug: msg: “{{ lookup(‘url’, ‘<https://adamtheautomator.com/resources/>’, username=’U’, password=’p’) }}”

Launch the command below to run the Ansible playbook (main3.yml), which will get data from a local page and a live website.

main3.yml ansible-playbook

You’ll see both tasks obtained and printed data (source code) of both the local page and the adamtheautomator.com website after a successful playbook execution.

Data Requests and Retrievals from Local and Live WebsitesData Requests and Retrievals from Local and Live Websites

Using Ansible Dictionary and lookup to return key/value pairs

You may need to specify certain environment variables in dictionaries format, which is key: value. It may be difficult to read such values, but the combination of Ansible lookup and Ansible dictionary does the task. Let’s see how it works.

In the “Searching for a File Using the Ansible Lookup” section, replace the text of the main.yaml playbook with the material below.

Ansible lookup gets the dictionary in one go in the Ansible Playbook below; then Ansible dictionary delivers a list with each item Ansible lookup requested.

# Declaring the variable users, which has two users in it (alistek and abertram) vars: alistek: users # users has two keys: name and phone number. Adamlistek and AdamBertram are the crucial name values. The important telephone numbers are 1234567890 and 1122334455. telephone: 1234567890 abertram: Adamlistek AdamBertram’s phone number is 1122334455. # Ansible task that displays the user’s information from the dictionary tasks using a lookup: – name: Employee phone numbers should be printed. debug: loop: “lookup(‘dict’, users)” msg: “User item.key is item.value.name (item.value.telephone)”

To execute the main.yml playbook, use the ansible-playbook command.

main.yml for ansible-playbook

The Ansible job outputs the dictionary elements with the values received from the users variable, as seen below.

In Dictionary Form, Returning Key/Value PairsIn Dictionary Form, Returning Key/Value Pairs

Using Ansible dig to get DNS records

You studied how the Ansible lookup plugin interacts with other Ansible modules including the Ansible Dictionary, Ansible debugs, and Ansible templates in the previous section. However, lots of other Ansible modules, such as the dig plugin, operate nicely with the Ansible Lookup. Let’s take a brief look at how to use Ansible dig.

Run the ansible-doc -t lookup -l command to get a list of all the Ansible lookup Plugins that are installed locally on your Ansible controller.

Replace the material in the previous section’s main.yaml playbook with the content below. Ansible dig obtains the DNS records for google.com that Ansible lookup requested in the Ansible Playbook below.

Ansible dig functionality with Ansible Lookup hosts: —- – name tasks: localhost # Executing tasks on the local computer – debug: msg: “lookup (‘dig’, ‘google.com’)” – name: Retrieving DNS records for google.com

The DNS record of google.com is presented on the output after you run ansible playbook as you can.

Google.com DNS record using Ansible Dig and LookupGoogle.com DNS record using Ansible Dig and Lookup

Conclusion

You’ve used the Ansible lookup parameter to read external data from a variety of external sources, such as website environment variables, using a single command in this lesson.

Which data do you aim to obtain next on your computer using the Ansible playbook now that you’ve mastered the Ansible lookup?

The “ansible lookup variable” is a command that allows users to integrate external data with the Ansible Lookup. Users can enter any text, and the value of the input will be displayed in Ansible output.

Related Tags

  • ansible lookup(‘file variable)
  • ansible lookup on remote host
  • ansible lookup yaml file
  • ansible lookup(‘url)
  • ansible lookup list

Table of Content