/bin/bash output in ansible playbook as a conditional statement

25 views
Skip to first unread message

Manish Kumar

unread,
Oct 11, 2019, 1:58:00 AM10/11/19
to Ansible Project

Dear All,

Is it possible to get the output from .sh file and compare with "Apache Tomcat/9.0.26" and if condition is true proceed for next step ?


---
- hosts: localhost
  tasks:
 
    - name: Extract archive
      shell:  sh /U01/apache-tomcat-9.0.26/bin/version.sh
      register: output
      when: output.stdout == "Apache Tomcat/9.0.26"
      debug: msg="passed"


Error Log:

[WARNING] Ansible is in a world writable directory (/U01/automation/playbooks), ignoring it as an ansible.cfg source.

 

PLAY [localhost] ***************************************************************

 

TASK [Gathering Facts] *********************************************************

ok: [localhost]

 

TASK [Extract archive] *********************************************************

fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'output.stdout == \"Apache Tomcat/9.0.26\"' failed. The error was: error while evaluating conditional (output.stdout == \"Apache Tomcat/9.0.26\"): 'output' is undefined\n\nThe error appears to have been in '/U01/automation/playbooks/Esm_java.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Extract archive\n      ^ here\n"}

Debugger invoked

(debug)


Thanks,
Manish

Stefan Hornburg (Racke)

unread,
Oct 11, 2019, 2:09:24 AM10/11/19
to ansible...@googlegroups.com
On 10/11/19 7:58 AM, Manish Kumar wrote:
>
> Dear All,
>
> Is it possible to get the output from .sh file and compare with "Apache Tomcat/9.0.26" and if condition is true proceed
> for next step ?
>

The condition in "when" is evaluated before running the task. You need an additional task where you can apply the
condition based on the values in output.stdout.

Regards
Racke

>
> ---
> - hosts: localhost
>   tasks:
>  
>     - name: Extract archive
>       shell:  sh /U01/apache-tomcat-9.0.26/bin/version.sh
>       register: output
>       when: output.stdout == "Apache Tomcat/9.0.26"
>       debug: msg="passed"
>
>
> Error Log:
>
> [WARNING] Ansible is in a world writable directory (/U01/automation/playbooks), ignoring it as an ansible.cfg source.
>
>  
>
> PLAY [localhost] ***************************************************************
>
>  
>
> TASK [Gathering Facts] *********************************************************
>
> ok: [localhost]
>
>  
>
> TASK [Extract archive] *********************************************************
>
> fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'output.stdout == \"Apache
> Tomcat/9.0.26\"' failed. The error was: error while evaluating conditional (output.stdout == \"Apache Tomcat/9.0.26\"):
> 'output' is undefined\n\nThe error appears to have been in '/U01/automation/playbooks/Esm_java.yml': line 5, column 7,
> but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    -
> name: Extract archive\n      ^ here\n"}
>
> Debugger invoked
>
> (debug)
>
>
> Thanks,
> Manish
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/78097434-3c2e-4b33-9747-a1b83d52a1a0%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/78097434-3c2e-4b33-9747-a1b83d52a1a0%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Manish Kumar

unread,
Oct 11, 2019, 2:19:04 AM10/11/19
to Ansible Project
Thanks Racke !!

Let me try it .
Reply all
Reply to author
Forward
0 new messages