# Ansible - variable to select artifact

**URL:** <https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228>\
**Category:** Ansible Project\
**Created:** [July 14, 2023, 6:00pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228 "2023-07-14T18:00:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Nadelson](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@David\_Nadelson](https://forum.ansible.com/u/David_Nadelson)\
**Post date:** [July 14, 2023, 6:00pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/1 "2023-07-14T18:00:22Z")

</div>

Hi All,  
I’m a DBA learning to use ansible to automate. I’m wondering if anyone can point me to docs on how to use ansible to select a file based on the value of a variable. i.e. if variable=x than choose 1.txt if variable=y then choose 2.txt.

Thanks,

David

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 17, 2023, 2:18pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/2 "2023-07-17T14:18:07Z")

</div>

Hi David,

Here is the basic of getting input from the user and reading the file -

```auto
# ls
1.txt 2.txt main.yml
# cat main.yml
---
- hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "{{ lookup('file', filenumber + '.txt') }}"

# ansible-playbook main.yml -e "filenumber=1"
...
TASK [debug] *****************************************************************************
ok: [localhost] => {
"msg": "one"
}
...

# ansible-playbook main.yml -e "filenumber=2"
...
TASK [debug] *****************************************************************************
ok: [localhost] => {
"msg": "two"
}
...

```

You can read more about -

1. [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html)
2. [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file\_lookup.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_lookup.html)
3. [https://docs.ansible.com/ansible/latest/reference\_appendices/general\_precedence.html#using-e-extra-variables-at-the-command-line](https://docs.ansible.com/ansible/latest/reference_appendices/general_precedence.html#using-e-extra-variables-at-the-command-line)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 18, 2023, 2:29am UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/3 "2023-07-18T02:29:36Z")

</div>

Hello, David.

You could use a lookup table implemented as a simple dict. If you only need it in one task, you can define it in that task’s vars section as I’ve done below, or farther “up” in host\_vars, group\_vars, etc., as long as it’s defined somewhere. Here’s a simple demo. I’m mapping colors to veggies, but you could map to file names or whatever. Be sure to handle the “missing” case.

```auto
[utoddl@tango ansible]$ **cat select-artifact.yml**
---
- name: Select artifact based on value
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Match an artifact based on a value
      ansible.builtin.debug:
        msg: "Value {{ item }} matches artifact {{ **artifacts[item]** | default('!missing!') }}."
      vars:
        **artifacts:**  **green: cucumbers**  **red: tomatoes**  **purple: cabage**  **yellow: squash**
      loop:
        - red
        - orange
        - yellow
        - green
        - blue
        - indigo
        - violet
[utoddl@tango ansible]$ **ansible-playbook select-artifact.yml**

PLAY [Select artifact based on value] *********************************************************

TASK [Match an artifact based on a value] *****************************************************
ok: [localhost] => (item=red) => {
    "msg": "Value red matches artifact tomatoes."
}
ok: [localhost] => (item=orange) => {
    "msg": "Value orange matches artifact !missing!."
}
ok: [localhost] => (item=yellow) => {
    "msg": "Value yellow matches artifact squash."
}
ok: [localhost] => (item=green) => {
    "msg": "Value green matches artifact cucumbers."
}
ok: [localhost] => (item=blue) => {
    "msg": "Value blue matches artifact !missing!."
}
ok: [localhost] => (item=indigo) => {
    "msg": "Value indigo matches artifact !missing!."
}
ok: [localhost] => (item=violet) => {
    "msg": "Value violet matches artifact !missing!."
}

PLAY RECAP ************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

```

Cheers,

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [July 18, 2023, 12:51pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/4 "2023-07-18T12:51:56Z")

</div>

Todd’s dictionary lookup is elegant and flexible. I’d go with that model.  
It supports any number of choices and gracefully gives a default value.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [July 18, 2023, 1:14pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/5 "2023-07-18T13:14:45Z")

</div>

If you didn’t want a default value and instead wanted to gracefully handle an invalid value you could do something like this:

- name: validate filename lookup value  
assert:  
that:

- my\_var in file\_table.keys()  
fail\_msg: “File lookup key ‘{{ my\_var }}’ is not valid”  
success\_msg: “File lookup key ‘{{ my\_var }}’ is valid”

- name: do my file-based task  
debug:  
msg: “File name is {{ artifacts[my\_var] }}”

The “assert” task will fail the playbook if any of the tests fail (you can list more than one test).  
If all the tests pass then the playbook continues to the next task.

Walter

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [July 18, 2023, 1:25pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/6 "2023-07-18T13:25:02Z")

</div>

Silly me … I should re-read my own code before pressing ‘send’!

> - name: validate filename lookup value  
> assert:  
> that:
> 
> - my\_var in file\_table.keys()  
> fail\_msg: “File lookup key ‘{{ my\_var }}’ is not valid”  
> success\_msg: “File lookup key ‘{{ my\_var }}’ is valid”
> 
> - name: do my file-based task  
> debug:  
> msg: “File name is {{file\_table[my\_var] }}”

An added footnote … keys() provides the dictionary keys in the lookup table as a list.

vars:  
file\_table:  
oracle: /apps/oracle/some/file/path  
mysql: /apps/mysql/other/path  
postgres: /apps/postgres/different/path

With this dictionary, file\_table.keys() returns a list of [mysql, oracle, postgres].The assert task tests my\_var against that list. If my\_var isn’t one of those values, the assert task fails. If my\_var is one of those values, the debug task prints the corresponding value associated with the key that my\_var provides. If my\_var is oracle, the debug prints /apps/oracle/some/file/path.

Walter

---

<div class="post-metadata">

**Author:** ![David\_Nadelson](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@David\_Nadelson](https://forum.ansible.com/u/David_Nadelson)\
**Post date:** [July 20, 2023, 4:01pm UTC](https://forum.ansible.com/t/ansible-variable-to-select-artifact/38228/7 "2023-07-20T16:01:25Z")

</div>

Thank you all so much for the responses.
