How can we write if else logic using shell or command.. I can not use when clauseas it does not give me expected result

26 views
Skip to first unread message

nishant bawane

unread,
Apr 11, 2017, 10:20:56 AM4/11/17
to Ansible Project
How can we write if else logic using shell or command.. I can not use when clauseas it does not give me expected result

J Hawkesworth

unread,
Apr 11, 2017, 12:23:10 PM4/11/17
to Ansible Project
What is it that you are trying to do? It might be possible to do another way.

Sometimes I use two set_fact tasks with mutually exclusive when conditions.

As long as you remember that you don't need "{{ }}" on your when conditions and the conditions evaluate to a boolean, when conditions can be useful.

Jon

nishant bawane

unread,
Apr 11, 2017, 1:33:31 PM4/11/17
to Ansible Project
Hi,

Can you please once review my question  -Ansible -Need to reset the value of variable before command.

I need assistance for that question, and i am trying different ways for it.

nishant bawane

unread,
Apr 11, 2017, 1:46:29 PM4/11/17
to Ansible Project
This is below question in which i need to reset the value of one of the variable -

Query: I need to fetch the latest java version from host servers and display the same on web page, using apache server which is installed on my local server(local server means server with ansible installed).

I am able to fetch the value, but need to reset the value of one of the variable to blank , below are the details.


I need to make it run time activity like ---

when i have Both Hosts : zlp12036 and zlp12037, it must show the java version for both on my local server which has apache installed
when i have Only one Host  i.e.zlp12036, it must show the java version for zlp12036 only
on my local server which has apache installed.

Host File Details -

[myserver]

zlp12036 ansible_ssh_host=zlp12036.vci.att.com ansible_ssh_user=abc
#zlp12037 ansible_ssh_host=zlp12037.vci.att.com ansible_ssh_user=abc

Playbook:

---
- hosts: myserver
  tasks:

  - name: Get the Host Name
    shell: host $(hostname -i) | awk '{print $NF }'|sed 's/\.$//'
    register: hosts
  - set_fact: the_hosts=hosts.stdout
  - debug: var={{the_hosts}}

  - name: Fetch Java Version
    shell: java -version 2>&1 | grep version | awk '{print $3}' | sed 's/"//g'
    register: result
  - set_fact: the_count=result.stdout
  - debug: var={{the_count}}

- hosts: localhost
  tasks:

  - debug: var=hostvars['zlp12036']['
result']['stdout']
  - debug: var=hostvars['zlp12037']['result']['stdout']
  - debug: var=hostvars['zlp12036']['hosts']['stdout']
  - debug: var=hostvars['zlp12037']['hosts']['stdout']

  - copy:
      content: "<table style='width:50%'align='center';border='1'>
                 <tr bgcolor='#DEB887'>
                   <td colspan='2'><center><b> JAVA VERSION FOR HOST SERVERS</b> <center></td>
                 </tr>
                 <tr bgcolor='#00FFFF'>
                   <th style='width:50%'>Server Name</th>
                   <th style='width:50%'>Current Java Version</th>
                 </tr>
                 <tr  bgcolor='#7FFFD4'>
                   <td style='width:50%'><center>{{ hostvars['zlp12036']['hosts']['stdout'] }}</center></td>
                   <td style='width:50%'><center>{{ hostvars['zlp12036']['result']['stdout'] }}<center></td>
                 </tr>
                 <tr  bgcolor='#7FFFD4'>
                   <td style='width:50%'><center>{{ hostvars['zlp12037']['hosts']['stdout'] }}</center></td>
                   <td style='width:50%'><center>{{ hostvars['zlp12037']['result']['stdout'] }}<center></td>
                 </tr>
                </table>"
      dest: /opt/app/capio/apache/users/htdocs/index.html


This works fine when in host file i have both the entries of zlp12036 and zlp12037, It fails when i comment one of the host server(zlp12037).

Error -
 TASK [debug] *******************************************************************
ok: [localhost] => {
    "hostvars['zlp12037']['hosts']['stdout']": "VARIABLE IS NOT DEFINED!"
----------------------------------------------------------------------------------------------------------------------------------------------------

I can understand the reason, as the zlp12037 is commented , so it wont have any value. can I make value of hostvars['zlp12037']['hosts']['stdout'] to null, so that we can overcome the exception and it should show the java version of zlp12036 only.
Or is there any workaround for this.
Any suggestion how can i achieve this.

Reply all
Reply to author
Forward
0 new messages