Bootstrap FreeKB - Ansible - lookup file (read local file)
Ansible - lookup file (read local file)

Updated:   |  Ansible articles

The lookup plugin is always executed on the control node (that's your Ansible server), which is to say that the lookup plugin does not do something on your managed nodes (the target systems).

To use the file plugin, the Python file.py script must be in your Ansible plugins lookup directory, such as /usr/lib/python3.12/site-packages/ansible/plugins/lookup/file.py.

The lookup plugin can be used to read a local file. In other words, the lookup plugin reads a file on the control node (that's your Ansible server). On the other hand, the slurp module can be used to read a file on a managed node (e.g. target system).

Let's say /tmp/foo.txt contains the following.

Line One
Line Two

 

The debug module can be used to print the contents of the file. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

- name: display the contents of foo.txt
  debug: 
    msg: "{{ lookup('file', '/tmp/foo.txt') }}"

 

Something like this should return the following.

TASK [display the contents of foo.txt]
ok: [localhost] => {
    "msg": "Line One\nLine Two"
}

 

Or you could store the contents of the file in a variable using the vars plugin.

vars:
  foo: "{{ lookup('file', '/tmp/foo.txt') }}"

 

Or using the set_fact module.

- set_fact:
    foo: "{{ lookup('file', 'foo.txt') }}"

 

The debug module can be used to print the variable.

- name: display the contents of the 'foo' variable
  debug: 
    var: foo

 

Which should return the following.

TASK [display the contents of the 'foo' variable]
ok: [localhost] => {
    "msg": "Line One\nLine Two"
}

 


Relative path

Let's say your playbook is named foo.yml, and the playbook exists in the /tmp/testing directory, like this.

/tmp/testing/foo.yml

 

The lookup plugin can accept relative directories. In this example, instead of using absolute directory /tmp/foo.txt, relative directory ../foo.txt is used.

vars:
  foo: "{{ lookup('file', '../foo.txt') }}"

 


File doesn't exist - expected paths - ignore errors

Let's say foo.txt is located at /tmp/foo.txt, but you use /var/foo.txt.

vars:
  foo: "{{ lookup('file', '/var/foo.txt') }}"

 

Something like this will be returned and the playbook will halt due to a fatal error.

[WARNING]: Unable to find '/var/foo.txt' in expected paths.

 

You can set errors to warning to output the warning but to let the playbook continue on.

vars:
  foo: "{{ lookup('file', '/var/foo.txt', errors='warn') }}"

 

Or you can ignore errors all together.

vars:
  foo: "{{ lookup('file', '/var/foo.txt', errors='ignore') }}"

 


looping through file

The loop parameter can be used to loop through each line in the file, like this.

- name: loop through each line in foo.txt
  debug: 
    msg: "{{ item }}"
  loop: "{{lookup('file', 'foo.txt') }}"

 

Or with the with_items parameter.

- name: loop through each line in foo.txt
  debug: 
    msg: "{{ item }}"
  with_items: "{{lookup('file', 'foo.txt') }}"

 

Or using the split filter.

- name: loop through each line in foo.txt
  debug:
    msg: "{{ lookup('file', 'foo.txt').split('\n') }}"

 

Running this play should return the following.

TASK [loop through each value in foo.txt]
ok: [localhost] => {
    "msg": "Line One"
}
ok: [localhost] => {
    "msg": "Line Two"
}

 


File delimiter

The split filter can be used to specify the delimiter to use (a single whitespace in this example)

- debug:
    msg: "{{ lookup('file', 'foo.txt').split(' ') }}"

 

Running this play should return the following.

TASK [debug]
ok: [localhost] => {
    "msg": "Line"
}
ok: [localhost] => {
    "msg": "One"
}
ok: [localhost] => {
    "msg": "Line"
}
ok: [localhost] => {
    "msg": "Two"
}

 

Index numbers can be used to return only a specific item.

- set_fact:
    foo: "{{ lookup('file', 'foo.txt').split('\n')[0] }}"

 

Running this play should return the following.

TASK [loop through each line in foo.txt]
ok: [localhost] => {
    "msg": [
      "Line"
    ]
}

Append lines in file to an array

By looping through each line in a file, each line can be appended to an array.

- name: append each line in foo.txt to the 'list' array
  set_fact: 
    list: "{{ item }}"
  with_items: "{{lookup('file', 'foo.txt') }}"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 004086 in the box below so that we can be sure you are a human.