<p>You need to use <code>set_fact</code> when setting a variable (instead of <code>register</code> the outcome of a task):</p>
<pre class="lang-yaml prettyprint-override"><code>- name: Set myParam from ini
  set_fact:
    myParam: &quot;{{ lookup('ini', 'section=mySection, file=/path_to/ini_file') }}&quot;

</code></pre>
<p>In the lookup:</p>
<ul>
<li><code>ini</code> specifies what lookup plugin we are using</li>
<li><code>section</code> and <code>file</code> are arguments passed to the lookup, according to <a href="https://docs.ansible.com/ansible/latest/plugins/lookup/ini.html" rel="nofollow noreferrer">its documentation</a></li>
</ul>

CC BY-SA 4.0
2020-07-06T12:34:48.587Z
The ini lookup seems to look for the ini_file only on the control host, but I want to look up a file on the remote host giving its full path, would that be possible?
a1an
CC BY-SA 4.0
2020-07-06T12:55:23.740Z
This may be possible if you use the [`delegate_to`](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#task) keyword - this should run the task on the delegated host, where the file is present. Else, you can `slurp` the file into a variable and read it from memory.
Bruce Becker
CC BY-SA 4.0
2020-07-06T13:35:32.173Z
As far as I understood, the delegate_to should be used the other way around (to run tasks on the local/controller machine or others) but the default for a task should be to run on the target controlled host so something seems not to be working as I would expect. is slurping the file compatible with the ini lookup (which expects a file)?
a1an
CC BY-SA 4.0
2020-07-06T11:25:32.370Z
2020-07-06T11:25:32.370Z
Bruce Becker
<p>Since the <code>ini</code> <a href="https://docs.ansible.com/ansible/latest/plugins/lookup.html" rel="nofollow noreferrer">lookup plugin only reads files local to the Control Host</a>, the remote file needs to be <code>fetch</code>ed locally before it can be read and <code>set_fact</code> has to be used to put its content into a variable, which results in the following:</p>
<pre class="lang-yaml prettyprint-override"><code>- block:

  - name: &quot;Retrieve remote ini file&quot;
    fetch:
      src: /path_to/ini_file
      dest: /tmp/ansible

  - name: &quot;Read and store my value&quot;
    set_fact:
      my_variable: &quot;{{ lookup( 'ini', 'myParam section=mySection file=/tmp/ansible/{{ inventory_hostname }}/path_to/ini_file' ) }}&quot;
</code></pre>
<p>Then the parameter is available as variable and can be used further in the playbook</p>
<pre class="lang-yaml prettyprint-override"><code>- debug:
    var: my_variable
</code></pre>

CC BY-SA 4.0
2023-01-10T23:26:59.087Z
This has been my quest for 6 weeks to find, and I finally find the answer in a stackexchange question from years ago. The fact this isn't in the documentation is CRIMINAL. Thank you so much for making this post!
Arcsector
CC BY-SA 4.0
2024-07-19T03:22:00.007Z
[By utilizing Custom Facts one can avoid `slurp` or `fetch`, lots of tasks like `set_fact`](https://stackoverflow.com/a/78752676/6771046)
U880D
CC BY-SA 4.0
2020-07-08T10:20:16.223Z
2024-07-19T04:54:04.743Z
2024-07-19T04:54:04.743Z
a1an
<p>I would like to set a variable value in a playbook reading it from an INI file, possibly failing the play if the file is not found or the value is missing in the file.</p>
<p>I have found the <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ini_lookup.html" rel="nofollow noreferrer"><code>ini</code> lookup plugin</a> to read values from INI files but it is not clear to me how to put that into a variable that I can reuse in other tasks without having to read it again.</p>
<p>I could think of something like:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: lookup variable from ini
  ini:
    name: myParam
    section: mySection
    file: /path_to/ini_file
  register: my_variable
</code></pre>
<p>But since <code>ini</code> is not a module this does not work.</p>
<p>Moreover, the INI file to be read from is located on the Remote System, the <code>ini</code> plugin however, seems to look on the Control Host for it (from the warning <code>Unable to find '/path_to/ini_file' in expected paths</code>).</p>

<ansible>
How to set a variable from an INI style file in Ansible?
a1an
# How to set a variable from an INI style file in Ansible?

(Asked by: username_0 on 2020-07-06)

I would like to set a variable value in a playbook reading it from an INI file, possibly failing the play if the file is not found or the value is missing in the file.
I have found the ini lookup plugin to read values from INI files but it is not clear to me how to put that into a variable that I can reuse in other tasks without having to read it again.
I could think of something like:
- name: lookup variable from ini
  ini:
    name: myParam
    section: mySection
    file: /path_to/ini_file
  register: my_variable

But since ini is not a module this does not work.
Moreover, the INI file to be read from is located on the Remote System, the ini plugin however, seems to look on the Control Host for it (from the warning Unable to find '/path_to/ini_file' in expected paths).

**Tags:** ansible


---

## 2 answers

### Answer by username_0 on 2020-07-08. Score: 9 | Accepted answer

Since the ini lookup plugin only reads files local to the Control Host, the remote file needs to be fetched locally before it can be read and set_fact has to be used to put its content into a variable, which results in the following:
- block:

  - name: "Retrieve remote ini file"
    fetch:
      src: /path_to/ini_file
      dest: /tmp/ansible

  - name: "Read and store my value"
    set_fact:
      my_variable: "{{ lookup( 'ini', 'myParam section=mySection file=/tmp/ansible/{{ inventory_hostname }}/path_to/ini_file' ) }}"

Then the parameter is available as variable and can be used further in the playbook
- debug:
    var: my_variable

> **Comments:**
> **username_2**: This has been my quest for 6 weeks to find, and I finally find the answer in a stackexchange question from years ago. The fact this isn't in the documentation is CRIMINAL. Thank you so much for making this post!  
> **username_3**: [By utilizing Custom Facts one can avoid `slurp` or `fetch`, lots of tasks like `set_fact`](https://stackoverflow.com/a/78752676/6771046)  

---

### Answer by username_1 on 2020-07-06. Score: 2

You need to use set_fact when setting a variable (instead of register the outcome of a task):
- name: Set myParam from ini
  set_fact:
    myParam: "{{ lookup('ini', 'section=mySection, file=/path_to/ini_file') }}"


In the lookup:

ini specifies what lookup plugin we are using
section and file are arguments passed to the lookup, according to its documentation

> **Comments:**
> **username_0**: The ini lookup seems to look for the ini_file only on the control host, but I want to look up a file on the remote host giving its full path, would that be possible?  
> **username_1**: This may be possible if you use the [`delegate_to`](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html#task) keyword - this should run the task on the delegated host, where the file is present. Else, you can `slurp` the file into a variable and read it from memory.  
> **username_0**: As far as I understood, the delegate_to should be used the other way around (to run tasks on the local/controller machine or others) but the default for a task should be to run on the target controlled host so something seems not to be working as I would expect. is slurping the file compatible with the ini lookup (which expects a file)?  

---

