2

I have inventory file as below,

[routers]
iLAB-SR-12-R2 ansible_host=192.168.82.211
iLAB-SR-12-R3 ansible_host=192.168.82.212
iLAB-SR-12-R4 ansible_host=192.168.82.213

And I have shell script for comparing multiple files with master file,

#!/bin/bash
config_location="/etc/ansible/test"
master="/etc/ansible/test-master"
cd $config_location
file=`ls -l | grep "^-" | awk '{print $NF}'`
echo "Hostname      IpAddress      Comments" > /etc/ansible/output.csv
for i in $file
do
  if diff -c $i $master > "/etc/ansible/test-difference/diff_$i" ;then
    echo "$i                    NoChange"
  else
    echo "$i                    SomeChange"
  fi
done >> /etc/ansible/output.csv

The question is if it was ansible i would use {{remote_host}} to get the ipaddress for my output.csv file. In script how could i call variable {{ansible_host}} to get ipaddress or is there any way.

Below is my sample output.csv file but the ipaddress is empty now,

Hostname   Ipaddress   Comments
Test1                   Somechanges
Test2                   Nochanges

Anyone help me how to get the ipaddress from the inventory file. Thanks in advance.

4
  • 1
    The way you are assigning your file-variable will fail you sooner or later. You should not parse ls , and I suggest naming the variable something other than "file", because file is a command name. You should quote your variables, too. File names can have spaces in them...
    – markgraf
    Commented Oct 8, 2019 at 11:27
  • You are echoing a files name, not its contents. So there can't be any IP-Adresses, unless you happen to have files named like IP-Adresses.
    – markgraf
    Commented Oct 8, 2019 at 11:30
  • 1
    Why are you using ansible as a data source and then reinventing its functionality? Do that logic in ansible and you can just interpolate variables. Im nort sure i understand what you're actually trying to accomplish though.
    – erik258
    Commented Oct 8, 2019 at 11:30
  • Seconded. ansible -i inventoryfile all -m setup -a 'filter=ansible_all_ipv4_addresses' will get you your ip-adresses.
    – markgraf
    Commented Oct 8, 2019 at 11:38

1 Answer 1

3

Can you modify your script ?

If you can, at the start of the script get the two parameters you need like:

remote_host="$1"
ansible_host="$2"

Then in your playbook, you can call your script this way:

myscript {{remote_host}} {{ansible_host}}

It's been a long time since I played with ansible. Make sure the syntax is right :)

5
  • 1
    Julien, Let me try and let you know. Thanks.
    – riswan66
    Commented Oct 9, 2019 at 7:27
  • @riswan66 sure :) Don't hesitate to edit my post in case I made a syntax error :)
    – Julien B.
    Commented Oct 9, 2019 at 12:13
  • Julien, If we call the variable as you mentioned means its getting the variable name but if we use with echo it doesnot print all the ipaddress instead print the 1 ipaddress repeated times. See below example, ``` ansible_host="$1" echo "Hostname IpAddress Comments" > /etc/ansible/output.csv for i in $file do if diff -c $i $master > "/etc/ansible/test-difference/diff_$i" ;then echo "$i $ansible_host NoChange" else echo "$i $ansible_host SomeChange" fi done >> /etc/ansible/output.csv ```
    – riswan66
    Commented Oct 11, 2019 at 2:10
  • The output we want is, Hostname Ipaddress Comments iLAB-SR-12-R2 192.168.82.211 NoChange iLAB-SR-12-R3 192.168.82.212 SomeChange But the output we get is, Hostname Ipaddress Comments iLAB-SR-12-R2 192.168.82.211 NoChange iLAB-SR-12-R3 192.168.82.211 SomeChange Means it reading the variables but cannot all the values. Script Execution: ./test.sh {{ansible_host}}
    – riswan66
    Commented Oct 11, 2019 at 2:14
  • @riswan66 Hum... from your comment, expected == actual; probably a copy paste issue, can you check that ?^^
    – Julien B.
    Commented Oct 11, 2019 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .