# ansible: set fact with multiple values

**URL:** <https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232>\
**Category:** Ansible Project\
**Created:** [August 2, 2018, 6:13am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232 "2018-08-02T06:13:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [August 2, 2018, 6:13am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/1 "2018-08-02T06:13:34Z")

</div>

i am writing an ansible role for setting some kind of “hostgroups” based on outputs from commands, path etc… thing is one host can be a member of more then one of such groups, and there is the problem. how to i append multiple values to one particular fact, without using a gazillion of different variables for the groups, as this list might get really long. someone over there at stackoverflow mentioned the ternary filter, but i can not get my head around it, so some example code wold be really great.

best would be a list like this:

“itsv\_hostgroup”: {WBG,LNZ,…}

in this example the WBG value is overwritten by the VIE value in the end.

`  
Ente

```auto
- shell: /usr/bin/uname -L | cut -d ' ' -f2
  register: itsv_lparname

- command: /usr/sbin/lsattr -El sys0 -a systemid -F value
  register: itsv_machine_serial

- set_fact:
        itsv_hostgroup: "LNZ"
        cacheable: true
  when: (itsv_machine_serial.stdout is match "IBM,02781A6BX") or
        (itsv_machine_serial.stdout is match "IBM,02781A6CX") or
        (itsv_machine_serial.stdout is match "IBM,02781A6DX")
- debug:
    var: itsv_hostgroup

- set_fact:
        itsv_hostgroup: "WBG"
        cacheable: true
  when: (itsv_machine_serial.stdout is match "IBM,022199BF7")
- debug:
    var: itsv_hostgroup

- set_fact:
        itsv_hostgroup: "GBG"
        cacheable: true
  when: (itsv_machine_serial.stdout is match "IBM,022199BB7")
- debug:
    var: itsv_hostgroup

- set_fact:
        itsv_hostgroup: "VIE"
        cacheable: true
  when: (itsv_machine_serial.stdout is match "IBM,022199BF7") or
        (itsv_machine_serial.stdout is match "022199BB7") or
        (itsv_machine_serial.stdout is match "IBM,02060CE6R")
- debug:
    var: itsv_hostgroup

root@lpgaixmgmtlx01:/etc/ansible/aix>ansible-playbook -i AIXWUKIT, testplay.yml

PLAY [run test play] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [AIXWUKIT]

TASK [hostgroups : shell] *************************************************************************************************************************************************************************************
changed: [AIXWUKIT]

TASK [hostgroups : command] ***********************************************************************************************************************************************************************************
changed: [AIXWUKIT]

TASK [hostgroups : set_fact] **********************************************************************************************************************************************************************************
skipping: [AIXWUKIT]

TASK [hostgroups : debug] *************************************************************************************************************************************************************************************
ok: [AIXWUKIT] => {
    "itsv_hostgroup": []
}

TASK [hostgroups : set_fact] **********************************************************************************************************************************************************************************
ok: [AIXWUKIT]

TASK [hostgroups : debug] *************************************************************************************************************************************************************************************
ok: [AIXWUKIT] => {
    "itsv_hostgroup": "WBG"
}

TASK [hostgroups : set_fact] **********************************************************************************************************************************************************************************
skipping: [AIXWUKIT]

TASK [hostgroups : debug] *************************************************************************************************************************************************************************************
ok: [AIXWUKIT] => {
    "itsv_hostgroup": "WBG"
}

TASK [hostgroups : set_fact] **********************************************************************************************************************************************************************************
ok: [AIXWUKIT]

TASK [hostgroups : debug] *************************************************************************************************************************************************************************************
ok: [AIXWUKIT] => {
    "itsv_hostgroup": "VIE"
}

TASK [print out the hostname of target] ***********************************************************************************************************************************************************************
changed: [AIXWUKIT]

```

r code here…  
`

---

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [August 2, 2018, 8:15am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/2 "2018-08-02T08:15:05Z")

</div>

i dont see any relation to my problem, seems like you posted to the wrong thread.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 2, 2018, 8:27am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/3 "2018-08-02T08:27:34Z")

</div>

Can you please stop hijacking other people thread and make your own.

This consider a very rude behavior.

Hopefully the moderators put you on moderation or ban you from the list.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 2, 2018, 8:49am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/4 "2018-08-02T08:49:34Z")

</div>

> best would be a list like this:
> 
> "itsv\_hostgroup": {WBG,LNZ,...}

Typo I guess list is is ["WBG","LNZ",...]

> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itsv\_hostgroup: "LNZ"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cacheable: true  
> &nbsp;&nbsp;when: (itsv\_machine\_serial.stdout is match "IBM,02781A6BX") or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(itsv\_machine\_serial.stdout is match "IBM,02781A6CX") or  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(itsv\_machine\_serial.stdout is match "IBM,02781A6DX")

To add just do this to all of them

&nbsp;&nbsp;&nbsp;itsv\_hostgroup: '{{ itsv\_hostgroup | default() + ["LNZ"] }}'

The default() is there so it doesn't fail when itsv\_hostgroup is not defined, it will then be set to a empty list .  
And the rest just add a element to the list.

---

<div class="post-metadata">

**Author:** ![christian.tremel](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@christian.tremel](https://forum.ansible.com/u/christian.tremel)\
**Post date:** [August 2, 2018, 9:36am UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/5 "2018-08-02T09:36:06Z")

</div>

hmm…this seems a little bit sophisticated as the list of groups is much longer in reality as in my example code which has only 4, so this leads to a big variable mess. no way to reference the group names in any other way?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 3, 2018, 4:54pm UTC](https://forum.ansible.com/t/ansible-set-fact-with-multiple-values/28232/6 "2018-08-03T16:54:57Z")

</div>

I don't think I understand the problem.  
Your are writing gazillion variables and big variable mess, what do you mean by that?

As I can see you only have to itsv\_hostgroup and itsv\_machine\_serial.stdout that's only two variable and very manageable in Ansible.
