I don't 100% understand your ultimate aim here. It might be easier to
understand if you break it down into smaller steps and include some sample
inputs and desired output?
A sanitised version of what's in my_instance, item_list , and what you
would like out at the end might help?
IF I understand what you're trying to do, using your loop item as the key
for your set_fact. For me, with appropriate quoting this seems to work...
$ cat wtf.yml
- hosts: localhost
become: no
gather_facts: no
vars:
varlist:
- value1
- value2
- valueC
tasks:
- name: Use item as the fact key for set_fact in a loop
set_fact:
"{{ item }}": value
loop: "{{ varlist }}"
- name: Check the values assigned to the facts
debug:
var: "{{ item }}"
loop: "{{ varlist }}"
$ ansible-playbook wtf.yml
PLAY [localhost]
*************************************************************************************************************************
TASK [Use item as the fact key for set_fact in a loop]
***********************************************************************************
ok: [localhost] => (item=value1)
ok: [localhost] => (item=value2)
ok: [localhost] => (item=valueC)
TASK [Check the values assigned to the facts]
********************************************************************************************
ok: [localhost] => (item=value1) => {
"ansible_loop_var": "item",
"item": "value1",
"value1": "value"
}
ok: [localhost] => (item=value2) => {
"ansible_loop_var": "item",
"item": "value2",
"value2": "value"
}
ok: [localhost] => (item=valueC) => {
"ansible_loop_var": "item",
"item": "valueC",
"valueC": "value"
}
PLAY RECAP
*******************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
On Thu, 3 Aug 2023 at 23:02, solarflow 99 <[email protected]> wrote:
> I wonder if something like this can be done, the problem i'm seeing is
> that the fact name has to be static, and could not use {{ item }}:
>
> - name: Set each tag to facts
> set_fact:
> list_of_vars: "{{ my_instance.instances |
> map(attribute='tags.list_of_vars') | list | join('\n') }}"
>
> - name: Split "list_of_vars" variable into a comma separated list
> set_fact:
> item_list: "{{ list_of_vars.split(',') }}"
>
> - name: lookup SSM Parameter Store secrets
> set_fact:
> * {{ item }}:* "{{ lookup('amazon.aws.ssm_parameter',
> puppet_env+'_'+env+'_{{ item }}', region=ansible_ec2_placement_region) }}"
> with_items: "{{ item_list }}"
>
>
> This would give me a fact for each comma separated var I put in the tag,
> if only it worked. Is there a better way to do something like this?
>
> --
> 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/b3642db8-15e2-406b-96ba-e49e0d4d50d8n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b3642db8-15e2-406b-96ba-e49e0d4d50d8n%40googlegroups.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/CAKtKohSoMGpjthtzU3afiKUOwo%3DZ9wxavQUTmvS1zXN4W_3kNw%40mail.gmail.com.