Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible conditional based on stdout of result?

Tags:

linux

ansible

How do I use the when statement based on the standard output of register: result? If standard output exists I want somecommand to run if no standard output exists I want someothercommand to run.

- hosts: myhosts   tasks:   - name: echo hello     command: echo hello     register: result   - command: somecommand {{ result.stdout }}     when: result|success   - command: someothercommand     when: result|failed 
like image 642
ibash Avatar asked Oct 01 '14 13:10

ibash


People also ask

Can Ansible playbook return value?

Ansible modules normally return a data structure that can be registered into a variable, or seen directly when output by the ansible program. Each module can optionally document its own unique return values (visible through ansible-doc and on the main docsite).

How do you store output of any task into a variable from playbook?

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


1 Answers

Try checking to see it if equals a blank string or not?

- hosts: myhosts   tasks:   - name: echo hello     command: echo hello     register: result   - command: somecommand {{ result.stdout }}     when: result.stdout != ""   - command: someothercommand     when: result.stdout == "" 
like image 60
R. S. Avatar answered Oct 29 '22 10:10

R. S.