Ansible get variable output

Hi all,

Thank you for reading my question. I am running into this issue and hope you can help me!

What we want to do is feed a tool with ansible to automate this. We have 99% ready, however the random password generation is still a bit difficult.
Our goal is to generate a random password per user, register this as a unique variable based on username and then use this variable to perform multiple actions such as mailing the password and creating the user with password.

We now run into the following issue:
The stdout of a dynamic variable is lost, and we cannot find the dynamic variable name to use it in our playbook. When we call the dynamic variable in the manner we created it we get various errors.

File information:

========= task file: main.yml ============

  • include_vars: foo.yml
    tags:

  • rogier

  • name: Generate a random password per user
    shell: /usr/bin/openssl passwd -1 -in /dev/urandom | head -1
    with_dict: users
    register: “{{item.username}}_result”
    tags:

  • rogier

========= end of task file ===============

========= output: =====================

TASK: [ON4_project_management | Generate a random password per user] ***********
changed: [m-opennebula4] => (item={‘key’: ‘Piet’, ‘value’: {‘username’: ‘pietu’, ‘telephone’: ‘987-654-3210’}}) => {“changed”: true, “cmd”: “/usr/bin/openssl passwd -1 -in /dev/urandom | head -1”, “delta”: “0:00:00.054796”, “end”: “2015-05-28 13:26:06.705414”, “item”: {“key”: “Piet”, “value”: {“telephone”: “987-654-3210”, “username”: “pietu”}}, “rc”: 0, “start”: “2015-05-28 13:26:06.650618”, “stderr”: “”, “stdout”: “$1$C2//4lrR$1/sC83CGFVXN6Ye3GafQ7.”, “warnings”: }
changed: [m-opennebula4] => (item={‘key’: ‘Klaas’, ‘value’: {‘username’: ‘klaasu’, ‘telephone’: ‘123-456-7890’}}) => {“changed”: true, “cmd”: “/usr/bin/openssl passwd -1 -in /dev/urandom | head -1”, “delta”: “0:00:00.222003”, “end”: “2015-05-28 13:26:07.258722”, “item”: {“key”: “Klaas”, “value”: {“telephone”: “123-456-7890”, “username”: “klaasu”}}, “rc”: 0, “start”: “2015-05-28 13:26:07.036719”, “stderr”: “”, “stdout”: “$1$BfLE8IoY$qKzqpyxuXHfw.pFIHIR461”, “warnings”: }

========= end of output ================

========= start dict file: foo.yml ==========

Hi,

iirc value for register are not passed via the templating engine, maybe this should work to get the username and thier associate password.

  • name: Generate a random password per user

shell: /usr/bin/openssl passwd -1 -in /dev/urandom | head -1

with_dict: users

register: foo

  • debug: msg={{ “username:” + item.item.key + “pass:” + item.stdout }}

with_items: foo.results

Hey Benno,

This answer is awesome! It works!
(We spend so many hours with nested variables and for loops and all kinds of tricks, this is short and sweet. THANKS!)

Thank you Benno.

Rogier