We have a playbook like this:
- hosts: localhost
gather_facts: false
vars:
produsers: [chris, dana]
stgusers: [pat, sandy]
allusers: [alex, andy, chris, dan, dana, jamie, pat, sandy, terry]
tasks:
- name: show authorized
debug: msg={{ item }}
when: item in lookup('flattened', [produsers, stgusers])
with_items: allusers
A list of users who should have access to prod, a list of users who should
have access to stg, and a list of all the users, including some who
shouldn't have access to either of those things. The task is then intended
to show the users who are in one list or the other. Thing is, it does this:
TASK: [show authorized] *******************************************************
skipping: [localhost] => (item=alex)
ok: [localhost] => (item=andy) => {
"item": "andy",
"msg": "andy"
}
ok: [localhost] => (item=chris) => {
"item": "chris",
"msg": "chris"
}
ok: [localhost] => (item=dan) => {
"item": "dan",
"msg": "dan"
}
ok: [localhost] => (item=dana) => {
"item": "dana",
"msg": "dana"
}
skipping: [localhost] => (item=jamie)
ok: [localhost] => (item=pat) => {
"item": "pat",
"msg": "pat"
}
ok: [localhost] => (item=sandy) => {
"item": "sandy",
"msg": "sandy"
}
skipping: [localhost] => (item=terry)
What are andy and dan doing there? They're not on the right lists! But,
their names *are* substrings of the names of some of the users who *are*
on the right lists. Hmm.
We tried this to debug:
- name: show flattened
debug: msg={{ item }}
with_items: lookup('flattened', [produsers, stgusers])
That says:
TASK: [show flattened] ********************************************************
ok: [localhost] => (item=chris,dana,pat,sandy) => {
"item": "chris,dana,pat,sandy",
"msg": "chris,dana,pat,sandy"
}
So, a list of users, right? Not so, because here's a similar one, but with
a literal list of users, not run lookup('flattened', list-of-lists):
- name: show literal
debug: msg={{ item }}
with_items: ['chris', 'dana', 'pat', 'sandy']
And that says
TASK: [show literal] **********************************************************
ok: [localhost] => (item=chris) => {
"item": "chris",
"msg": "chris"
}
ok: [localhost] => (item=dana) => {
"item": "dana",
"msg": "dana"
}
ok: [localhost] => (item=pat) => {
"item": "pat",
"msg": "pat"
}
ok: [localhost] => (item=sandy) => {
"item": "sandy",
"msg": "sandy"
}
So it looks like lookup('flattened', list-of-lists) is returning a
*string* of comma-separated values, rather than a list of values.
with_flattened seems to return a list of values, but we're not sure how to
use that in this context, because we already need to with_items over
allusers.
What's the best way to do this? Is there a lookup plugin that works like
with_flattened, i.e. returns a list of values rather than a string?
-Josh (jbs@care.com)
This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.