Ansible get variable output

78 views
Skip to first unread message

R. Dikkes

unread,
May 28, 2015, 8:04:42 AM5/28/15
to ansible...@googlegroups.com
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 ==========
--- 
users:
  Klaas:
    username: klaasu
    telephone: 123-456-7890
  Piet:
    username: pietu
    telephone: 987-654-3210
===================================


Do you have any suggestion how we can be able to use this dynamic variable.stdout?

benno joy

unread,
May 28, 2015, 8:24:56 AM5/28/15
to ansible...@googlegroups.com
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




--
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/3e1093ae-8c97-4de6-8292-1cbbe3610f32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

R. Dikkes

unread,
May 28, 2015, 8:54:29 AM5/28/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages