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 On Jul 17, 2023, at 10:29 PM, Todd Lewis <[email protected]> wrote: 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 On 7/14/23 2:00 PM, David Nadelson wrote: 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 -- 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 [email protected]<mailto:[email protected]>. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2927e3e7-9dae-26e1-d51c-e42a3acd6607%40gmail.com<https://groups.google.com/d/msgid/ansible-project/2927e3e7-9dae-26e1-d51c-e42a3acd6607%40gmail.com?utm_medium=email&utm_source=footer>. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9B6013A3-11F0-4D69-B9E6-56BAEC15EA40%40nist.gov.
