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.