Variable indirection

23 views
Skip to first unread message

eric.b....@nasa.gov

unread,
Sep 25, 2018, 11:30:53 AM9/25/18
to Ansible Project
Greetings.

I feel like I've done this before, but for some reason, I can't figure it out.

I've got two hashes defined.

password_change:
  cobbler: [ ebhpw ]
  cougar: [ ehymowitz ]

password_groups:
  ebhpw: ehymowitz

Then I've got a play.

  - name: eric testing 9
    debug:
      msg: "it worked!!!! variable is {{password_change.cobbler}}"
    when:
      - password_change.cobbler is defined

This works:

TASK [eric testing 9]
ok: [gs-444-e10285] => {
    "msg": "it worked!!!! variable is [u'ebhpw']"
}

I don't fully understand what the brackets and the u are for.  But I think that's getting in my way.  Because now I'm trying to do this:

  - name: eric testing 9
    debug:
      msg: "it worked!!!! variable is {{password_change.cobbler}}"
    when:
     - password_change.cobbler is defined
     - password_groups[password_change.cobbler] is defined

and it fails.

TASK [eric testing 9]
skipping: [gs-444-e10285]

So what am I missing?  It knows what password_change.cobbler is ("ebhpw") , and it knows what password_groups[ebhpw] is ("ehymowitz"), so why I can't I properly test for the value of password_groups[password_change.cobbler] ?

Thanks.

--EbH

==========

I don't know if this is related or not, but I'll put it here just in case.

I've got this inside of a play:
    lineinfile:
      path: "/var/lib/cobbler/snippets/password_{{password_change[inventory_hostname]}}"

The file /var/lib/cobbler/snippets/password_ehymowitz definitely exists ... but ansible is complaining that "Destination /var/lib/cobbler/snippets/password_[u'ehymowitz'] does not exist"    .  Again with the brackets and the u .  I don't understand what that is, or why I'm getting it, or how to get rid of it.  I don't know if that's causing the above problem.

Kai Stian Olstad

unread,
Sep 28, 2018, 3:51:30 PM9/28/18
to ansible...@googlegroups.com
On Tuesday, 25 September 2018 17.30.53 CEST eric.b.hymowitz via Ansible Project wrote:
> Greetings.
>
> I feel like I've done this before, but for some reason, I can't figure it
> out.
>
> I've got two hashes defined.
>
> password_change:
> cobbler: [ ebhpw ]
> cougar: [ ehymowitz ]

If you don't understand the brackets, why are you using them here?


> password_groups:
> ebhpw: ehymowitz
>
> Then I've got a play.
>
> - name: eric testing 9
> debug:
> msg: "it worked!!!! variable is {{password_change.cobbler}}"
> when:
> - password_change.cobbler is defined
>
> This works:
>
> TASK [eric testing 9]
> ok: [gs-444-e10285] => {
> "msg": "it worked!!!! variable is [u'ebhpw']"
> }
>
> I don't fully understand what the brackets and the u are for.

List(array) in Ansible can be written in two ways.

myvar: ["one","two"]

or

myvar:
- one
- two

u is Pythons way of saying that the element in the list is unicode.


> But I think
> that's getting in my way. Because now I'm trying to do this:

Then you shouldn't use them in the first place.


> - name: eric testing 9
> debug:
> msg: "it worked!!!! variable is {{password_change.cobbler}}"
> when:
> - password_change.cobbler is defined
> - password_groups[password_change.cobbler] is defined
>
> and it fails.
>
> TASK [eric testing 9]
> skipping: [gs-444-e10285]
>
> So what am I missing? It knows what password_change.cobbler is ("ebhpw") ,
> and it knows what password_groups[ebhpw] is ("ehymowitz"), so why I can't I
> properly test for the value of password_groups[password_change.cobbler] ?

password_change.cobbler is not "ebhpw" it's list since you have the values between [], but password_change.cobbler.0 does contain "ebhpw"


--
Kai Stian Olstad


Hymowitz, Eric B. (GSFC-444.0)[KBRwyle]

unread,
Oct 1, 2018, 7:52:19 AM10/1/18
to ansible...@googlegroups.com
Thank you very much. That was the little push I needed. I think I've got it now.

--EbH
Reply all
Reply to author
Forward
0 new messages