ansible: set fact with multiple values

843 views
Skip to first unread message

christia...@itsv.at

unread,
Aug 2, 2018, 2:13:34 AM8/2/18
to Ansible Project

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
- 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...


Message has been deleted

christia...@itsv.at

unread,
Aug 2, 2018, 4:15:05 AM8/2/18
to Ansible Project
i dont see any relation to my problem, seems like you posted to the wrong thread.

On Thursday, August 2, 2018 at 9:54:45 AM UTC+2, Badraj Angirekula wrote:
Hi all I am facing error in aws please help.


[root@server2 ~]# ssh -i bhadra.pem ec2-...@ec2-50-112-6-190.us-west-2.compute.                                                                                                                                                             amazonaws.com
The authenticity of host 'ec2-50-112-6-190.us-west-2.compute.amazonaws.com (50.1                                                                                                                                                             12.6.190)' can't be established.
ECDSA key fingerprint is 1f:1b:6b:13:d2:7a:b5:a4:4b:78:84:b7:98:05:ab:39.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ec2-50-112-6-190.us-west-2.compute.amazonaws.com,50.                                                                                                                                                             112.6.190' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'bhadra.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: bhadra.pem
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Thanks & Regards,
BADRAJ ANGIREKULA

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/501beb48-c9aa-44ea-a7f2-39eb6115044a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Aug 2, 2018, 4:27:45 AM8/2/18
to ansible...@googlegroups.com
On 02.08.2018 09:54, Badraj Angirekula wrote:
> Hi all I am facing error in aws please help.

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.

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Aug 2, 2018, 4:49:42 AM8/2/18
to ansible...@googlegroups.com
On 02.08.2018 08:13, christia...@itsv.at wrote:
> best would be a list like this:
>
> "itsv_hostgroup": {WBG,LNZ,...}

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


> - 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")

To add just do this to all of them

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.

--
Kai Stian Olstad

christia...@itsv.at

unread,
Aug 2, 2018, 5:36:06 AM8/2/18
to Ansible Project
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?

Kai Stian Olstad

unread,
Aug 3, 2018, 12:55:08 PM8/3/18
to ansible...@googlegroups.com
On 02.08.2018 11:36, christia...@itsv.at wrote:
> 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?

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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages