MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Deep Dive into Lookup Plugins in Ansible with Example

Lets understand about Lookup Plugins

  • Lookup plugins allow Ansible to access data from outside sources. This can include reading the filesystem in addition to contacting external datastores and services.
  • Like all templating, these plugins are evaluated on the Ansible control machine, not on the target/remote i.e Lookups occur on the local computer, not on the remote computer.
  • The data returned by a lookup plugin is made available using the standard templating system in Ansible, and are typically used to load variables or templates with information from those systems.
  • Lookups are an Ansible-specific extension to the Jinja2 templating language.
  • They are executed within the directory containing the role or play, as opposed to local tasks which are executed with the directory of the executed script.
  • You can pass wantlist=True to lookups to use in jinja2 template “for” loops.

These are used mainly by the template engine inside Ansible. They’re used in two ways.

First, in a function syntax to load external information:

{{lookup(pipe’,/usr/bin/whoami’)}}
{{lookup(etcd’,somekey’)}} – this allows you to fetch a key out of an NCD store.

Second, lookup plugins are also the source of with loops (with_items loads the items.py lookup plugin). Furthermore, they are always expected to return a list of items, because of their potential use with loops.

Purpose of Ansible Lookups

When it comes to automation, we handle different types of data and files such as csv, txt and sometimes we might even need to read data from a key-value store such as etcd or redis. That where the ansible lookup plugins are useful.

Location of lookup plugins directory

  • Default location: ~/.ansible/plugins/lookup:/usr/share/ansible/plugins/lookup
  • Another Location: ansible/plugins/lookup directory inside current ansible python package
  • Or with ANSIBLE_LOOKUP_PLUGINS environment variable

Enabling Custom Lookup Plugins

You can activate a custom lookup by either dropping it into…

  • A lookup_plugins directory adjacent to your play
  • inside a role, or
  • By putting it in one of the lookup directory sources configured in ansible.cfg.

How to Using Lookup Plugins?

Lookup plugins can be used anywhere you can use templating in Ansible:

  • in a play,
  • in variables file, or
  • in a Jinja2 template for the template module.

Command line to see Plugin List?


To see the list of available plugins.
$ ansible-doc -t lookup -l 

To see specific documents and examples
$ ansible-doc -t lookup <plugin name>Code language: HTML, XML (xml)

List of Lookup Plugins List

  • aws_account_attribute – Look up AWS account attributes.
  • aws_service_ip_ranges – Look up the IP ranges for services provided in AWS such as EC2 and S3.
  • aws_ssm – Get the value for a SSM parameter or all parameters under a path.
  • cartesian – returns the cartesian product of lists
  • chef_databag – fetches data from a Chef Databag
  • config – Lookup current Ansible configuration values
  • conjur_variable – Fetch credentials from CyberArk Conjur.
  • consul_kv – Fetch metadata from a Consul key value store.
  • credstash – retrieve secrets from Credstash on AWS
  • csvfile – read data from a TSV or CSV file
  • cyberarkpassword – get secrets from CyberArk AIM
  • dict – returns key/value pair items from dictionaries
  • dig – query DNS using the dnspython library
  • dnstxt – query a domain(s)’s DNS txt fields
  • env – read the value of environment variables
  • etcd – get info from etcd server
  • file – read file contents
  • fileglob – list files matching a pattern
  • filetree – recursively match all files in a directory tree
  • first_found – return first file found from list
  • flattened – return single list completely flattened
  • hashi_vault – retrieve secrets from HashiCorp’s vault
  • hiera – get info from hiera data
  • indexed_items – rewrites lists to return ‘indexed items’
  • ini – read data from a ini file
  • inventory_hostnames – list of inventory hosts matching a host pattern
  • items – list of items
  • k8s – Query the K8s API
  • keyring – grab secrets from the OS keyring
  • lastpass – fetch data from lastpass
  • lines – read lines from command
  • list – simply returns what it is given.
  • mongodb – lookup info from MongoDB
  • nested – composes a list with nested elements of other lists
  • nios – Query Infoblox NIOS objects
  • nios_next_ip – Return the next available IP address for a network
  • openshift – Query the OpenShift API
  • password – retrieve or generate a random password, stored in a file
  • passwordstore – manage passwords with passwordstore.org’s pass utility
  • pipe – read output from a command
  • random_choice – return random element from list
  • redis – fetch data from Redis
  • redis_kv – fetch data from Redis
  • sequence – generate a list based on a number sequence
  • shelvefile – read keys from Python shelve file
  • subelements – traverse nested key from a list of dictionaries
  • template – retrieve contents of file after templating with Jinja2
  • together – merges lists into syncronized list
  • url – return contents from URL
  • vars – Lookup templated value of variables

Method to invoke lookup plugins

  • Method 1 – using lookup
  • Method 2 – Using query

In Ansible 2.5, a new jinja2 function called query was added for invoking lookup plugins. The difference between lookup and query is largely that query will always return a list. The default behavior of lookup is to return a string of comma separated values. lookup can be explicitly configured to return a list using wantlist=True.

The following examples are equivalent:

lookup(‘dict’, dict_variable, wantlist=True)
query(‘dict’, dict_variable)

Example of Lookups Plugins

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Ansible Playbook for Zabbix Agent Deployment

This page shows an Ansible playbook that installs and configures Zabbix agent 6.0 on CentOS and RHEL hosts. Below the original playbook you will find a corrected,…

Read More

How Ansible Automates Cisco IOS and IOS XE Network Devices

Introduction Managing multiple Cisco routers and switches manually can become time-consuming as a network grows. Network engineers regularly configure interfaces, check device status, update settings, and troubleshoot…

Read More

Configuration Management in DevOps: Concepts, Tooling, and a Practical Workflow

This guide explains how configuration management works in modern DevOps teams, compares common tools, and shows a practical Ansible example. It also preserves the original SlideShare reference from the legacy post.

Read More

Configuration Management in DevOps: Workflow, Tool Comparison, and an Ansible Example

This guide explains configuration management for DevOps teams: what it solves, how it fits into delivery workflows, and how major tool options compare. It also includes a practical Ansible example you can adapt for real environments.

Read More

Infrastructure as Code: A Practical Guide for DevOps Professionals

Introduction Managing infrastructure has undergone a massive transformation over the past decade. If you started your career in the era of physical data centers, you likely remember…

Read More

Staff Network Automation Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path

The Staff Network Automation Engineer is a senior individual contributor in the Cloud & Infrastructure organization responsible for designing, building, and scaling automation systems that make network provisioning, configuration, validation, and operations reliable, fast, and repeatable. The role blends deep networking fundamentals with software engineering practices (version control, CI/CD, testing, observability) to deliver “network as code” capabilities across data center, cloud, and edge environments.

Read More