↓ Skip to main content
Linux

`lookup`: Ansible

·3 mins

Ansible’s lookup plugin allows you to access data from a variety of sources, including files, templates, and external programs. You can use lookup to fetch data dynamically at runtime and use it in your playbooks and roles.

Ansible lookup plugin reading from files, environment variables, templates or programs and returning a value
Recreated in October 2026 from the post; the original image was lost

The lookup plugin is used in the following format:

{{ lookup('<plugin>', '<plugin arguments>') }}

Here, <plugin> is the name of the plugin you want to use, and <plugin arguments> are any arguments that the plugin requires. The result of the plugin is then returned as a variable.

In this blog post, we will explore how to use the lookup plugin in Ansible, along with some examples of when and how to use it.

Using Lookup with When Condition
#

One of the most common use cases of the lookup plugin is to conditionally execute a task based on the value of a variable. You can use the when keyword to specify a condition that determines whether or not the task should be executed.

Here’s an example of how to use lookup with a when condition:

- name: Copy file based on OS
  copy:
    src: "{{ lookup('env', 'HOME') }}/file.{{ 'exe' if ansible_os_family == 'Windows' else 'sh' }}"
    dest: /path/to/file
  when: ansible_os_family == 'Windows' or ansible_os_family == 'Linux'

In this example, we use lookup to retrieve the value of the HOME environment variable, and then concatenate it with the appropriate file extension based on the operating system. We then use this value as the source file for the copy module. The task is conditionally executed based on the value of the ansible_os_family variable.

Using Lookup to Read Data from a File
#

Another common use case of the lookup plugin is to read data from a file and use it in a task. You can use the file plugin to read data from a file and store it as a variable.

Here’s an example of how to use lookup with the file plugin:

- name: Read data from file
  set_fact:
    data: "{{ lookup('file', '/path/to/file') }}"

In this example, we use lookup to read the contents of a file located at /path/to/file. The contents of the file are then stored as a variable named data.

Using Lookup with Loop
#

You can also use the lookup plugin with a loop to iterate over a list of values and perform a task for each value. The with_items keyword is used to specify the list of values.

Here’s an example of how to use lookup with a loop:

- name: Create users
  user:
    name: "{{ item }}"
  with_items: "{{ lookup('file', '/path/to/userlist.txt').split('\n') }}"

In this example, we use lookup to read the contents of a file located at /path/to/userlist.txt, split the contents into a list of values, and then iterate over each value using a loop. For each value in the list, we create a user account using the user module.

Using Lookup with Templating
#

You can also use the lookup plugin with templating to dynamically generate values at runtime. You can use variables, filters, and other Jinja2 templating features to customize the output of the lookup plugin.

Here’s an example of how to use lookup with templating:

- name: Create directories
  file:
    path: "{{ lookup('env', 'HOME') }}/{{ item }}/{{
Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Python Virtual Enviroments

·3 mins
Recreated in October 2026 from the post; the original image was lost Using a virtual environment like venv is a best practice when working with Python-based projects like Ansible, Molecule, and Playbooks. Continuing from the blog, Virtual environments are isolated Python environments that allow you to manage dependencies separately for each project, providing a controlled and reproducible environment for development, testing, and production.

VENV: Ansible

·2 mins
Recreated in October 2026 from the post; the original image was lost Ansible and Molecule are popular tools for configuration management and testing in the world of DevOps. To avoid conflicts between different projects and their dependencies, it is recommended to use a virtual environment for each project. In this blog post, we’ll show you how to set up and use a virtual environment using Python’s built-in venv module on macOS and CentOS 7.

Ansible Configuration: `ansible.cfg`

·2 mins
Recreated in October 2026 from the post; the original image was lost ansible.cfg is the main configuration file for Ansible. It is used to customize Ansible’s behavior and includes a wide range of options, including the location of inventory files, the default SSH user, and the path to the module library.