Ansible - variable to select artifact

37 views
Skip to first unread message

David Nadelson

unread,
Jul 17, 2023, 9:41:03 AM7/17/23
to Ansible Project
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 

Abhijeet Kasurde

unread,
Jul 17, 2023, 10:18:35 AM7/17/23
to ansible...@googlegroups.com
Hi David,

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

```
# 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 -

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b52e8df2-34bf-4807-b68b-e54cf6386390n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Todd Lewis

unread,
Jul 17, 2023, 10:30:41 PM7/17/23
to ansible...@googlegroups.com, uto...@gmail.com
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.
[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,
--
Todd

Rowe, Walter P. (Fed)

unread,
Jul 18, 2023, 8:52:12 AM7/18/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Jul 18, 2023, 9:15:09 AM7/18/23
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

Rowe, Walter P. (Fed)

unread,
Jul 18, 2023, 9:25:23 AM7/18/23
to 'Rowe, Walter P. (Fed)' via Ansible Project
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

David Nadelson

unread,
Jul 24, 2023, 3:11:36 PM7/24/23
to Ansible Project
Thank you all so much for the responses. 
Reply all
Reply to author
Forward
0 new messages