Loop through hosts with ansible

Question:

I have a problem to find a working solution to loop over my inventory.
I start my playbook with linking a intentory file:

ansible-playbook -i inventory/dev.yml playbook.yml

My playbook looks like this:

So when loading the playbook the variable enviro is read from host_vars and sets the when condition to dev. The inventory file dev.yml looks like this:

My main.yml in my role local_vm_creator looks like this:

And the problem is that i can’t get the listed servers from the dev.yml inventory file.

it just returns:

ok: [localhost] => (item=groups[‘local_vm’]) => {
“item”: “groups[‘local_vm’]”,
“msg”: “host is groups[‘local_vm’]” }

Answer:

If the only problem is with_items loop, replace it with:

and you are good to go. Bare variables are not supported in with_ any more.

Leave a Reply