Ansible lookup file - How to Read file into variable in Ansible

In this post, we are going to see how to use ansible lookup and how to read file  into ansible playbook using ansible file lookup.

Purpose of Ansible Lookups

When it comes to automation, we handle different types of data and files such as csvtxt 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.

Ansible Lookup plugins allow Ansible to access data from outside sources. This can include reading the filesystem in addition to contacting external datastores and services.

 

Popular Ansible Lookups

There are various Ansible Lookup plugins available and we have shortlisted some of the widely used and popular ansible lookup plugins or simply ansible lookups

However, we are going to see only the file lookup in this article.

Name Source of Information/ Purpose
file Content of File
password Randomly Generate a password
pipe The output of Locally executed Command
env Environment Variable
template Jinja2 template after evaluation
csvfile Entry in a .csv file
dnxtxt DNS TXT record
redis_kv Redis Key Lookup
etcd ETCD key Lookup

Ansible File Lookup Example

Simply put, Ansible file lookup helps to read the file content and load or display within the Ansible playbook.  with Ansible file lookup you can read a file and assign to a variable for further processing.

in the following example,  you could notice that the task1 and task2 are doing the exact same job of copying the public key from local and adding to the authorized_key on the remote server to enable SSH Key based authentication.

The First task, you could see that the public Key has been passed as a bare text to the key parameter. In the second task, the same job is accomplished more efficiently with the help of Ansible File Lookup.

As you could have understood by now Ansible file lookup helps to read the file content.

 

- – 
 - name: Ansible Lookup examples
   hosts: app
   become: true
   tasks: 
    # BOTH TASKS ARE EXACLY DOING THE SAME JOB
    # IN TASK1: WE ARE KEEPING THE PUBLIC KEY AS A TEXT IN PLAYBOOK
    - name: "Copy the public Key Using the Key directly within Playbook"
      authorized_key:
        user: vagrant
        state: present
        key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmdlM0QV6HxZQ7iqXiboefzMHR/cKX+qlTezRiExW6jmwNaD1a9F3KlMaoi4eTKGtPbM9eTygcBjJOGZhEjZITuNat7teK/evvbiszrT3ORUvotnv8yjVC02CGFsK6fPs10J0rgITPdsnd+oq9WcJ/2rM5wHJPoSfhUzhgDB7mdOIeVM+mG89j+OPV377HRTyC5O9Ja9nX9J5ElHXFWu2CTLMjgYxZ16FLpIdlrL4I12mCucZ8jGaZp8frarwyilHsuUt2hQFi3XEmT3ACKiAtE0kBhclr2gtc2wNoVJVoWB [email protected]"

    # IN TASK2: WE ARE READING THE PUBLIC KEY FROM THE FILE DIRECTLY USING LOOKUP
    # ANSIBLE FILE LOOKUP PLUGIN HELPS TO READ THE FILE CONTENTS WITHIN THE ANSIBLE PLAYBOOK
    - name: "Copy the public Key using Lookup"
      authorized_key:
        user: vagrant
        state: present
        key: "{{ lookup(‘file’, ‘~/.ssh/id_rsa.pub’) }}"

Execution Result of this Ansible lookup playbook

You can see the task1 is executed successfully so the task2 is not making any further changes and we are able to SSH in without being prompted for the password.

vagrant machines forward the ssh port 22 into various ports like 2200 and 2222 that is what am using in this screenshot if you are wondering.

Ansible lookup file

Further Real time example of this Ansible lookup plugin

Hope this article helps.

Cheers
Sarav

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading