Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - pass output of shell command to variable

Tags:

ansible

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 [email protected] -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.

like image 565
nbucko Avatar asked Jan 25 '18 20:01

nbucko


People also ask

How do you store output of a command in a variable in Ansible?

Create the playbook to execute the “df” command to check the /boot usage. Use “register” to store the output to a variable.

How do you pass a command in Ansible playbook?

The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.


1 Answers

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 [email protected] -mailbox true -passwd {{ apg_generate.stdout }}"
like image 170
Konstantin Suvorov Avatar answered Oct 13 '22 11:10

Konstantin Suvorov