Hey,
if i need set_fact for a list of python script is it possible?
You’d like to store a list in a var using set_fact
module, is that it ? If so, yes you can. Check examples on module doc page.
Now your example output would look more like a list of dicts, because for a pure dict, you would then have duplicate keys. So:
result1:
- AP-ID: 1
AP Name: AP01
- AP-ID: 2
AP Name: AP02
- AP-ID: 3
AP Name: AP03
In the case your script gives you exactly this output:
AP-ID: 1
AP Name: AP01
AP-ID: 2
AP Name: AP02
AP-ID: 3
AP Name: AP03
You would need to parse it before or while storing it in a var, adding each item pair (AP-ID and AP Name) as individual list item. Tell me if you’d like me to give you some snippet.
Another solution would be to parse it raw with awk
, sed
, … beforehand, or see if your script could output each item individually. Depends on your usecase really.
how can i bind AP-ID value and AP name together?
There are multiple ways to do so and I’m not very good with data manipulation, but here is an example:
---
- name: Test Playbook
gather_facts: no
connection: local
hosts: localhost
vars:
result1:
- AP-ID: 1
AP Name: AP01
- AP-ID: 2
AP Name: AP02
- AP-ID: 3
AP Name: AP03
tasks:
- name: Combine dict values
ansible.builtin.debug:
msg: "{{ item.values()|join(',') }}"
loop: "{{ result1 }}"
Output:
PLAY [Test Playbook] *******************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Combine dict values] *************************************************************************************************************************************************************************************************************************************************************************************************
Tuesday 07 November 2023 18:24:07 +0100 (0:00:00.008) 0:00:00.008 ******
ok: [localhost] => (item={'AP-ID': 1, 'AP Name': 'AP01'}) => {
"msg": "1,AP01"
}
ok: [localhost] => (item={'AP-ID': 2, 'AP Name': 'AP02'}) => {
"msg": "2,AP02"
}
ok: [localhost] => (item={'AP-ID': 3, 'AP Name': 'AP03'}) => {
"msg": "3,AP03"
}
I’m not sure if it’s precisely what you’re looking for; if not, please provide more context and I’ll see what I can do.