6

New to ansible and playbooks, I'm trying to run a linux command and use the output of that command as a variable. However, it is using the item name as the variable instead of the output of the command.

- name: Use apg to generate a password
command: apg -m 12 -n 1 -a 1
register: apg_generate

- name: Create Mail Account
command: "plesk bin mail --create admin@test.com -mailbox true -passwd {{ item }}"
with_items: apg_generate.stdout

Instead of using the output of the apg command, which would be a random set of 12 characters I'm getting apg_generate.stdout as the password being set.

Dhaval Jardosh
  • 7,151
  • 5
  • 27
  • 69
nbucko
  • 69
  • 1
  • 1
  • 9
  • Duplicate of [How to use Ansible's with_item with a variable?](https://stackoverflow.com/q/33980776/2947502), but even if you quote the variable, you cannot iterate over `stdout`, because it is a string. The whole idea makes no sense. It's fine to be new to some topic, but in such situation start following docs and basic tutorials instead of posting bad questions on SO. – techraf Jan 25 '18 at 20:18

1 Answers1

9

In Ansible with_items is for loops, you don't need to use it if you want to access just a single variable. Access it directly:

- name: Use apg to generate a password
  command: apg -m 12 -n 1 -a 1
  register: apg_generate

- name: Create Mail Account
  command: "plesk bin mail --create admin@test.com -mailbox true -passwd {{ apg_generate.stdout }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193