I have three linux servers and i created the ansible inventory file:
[web]
192.168.0.155
192.168.0.165
192.168.0.175
And i have playbook.yml:
---
- hosts: web
tasks:
- name: Check drinks versions
shell: "python3.4 {{ item.sw_path }} -v"
sudo: yes
with_items:
- { sw_path: '/home/beer.py' }
- { sw_path: '/home/vodka.py' }
- { sw_path: '/home/whisky.py' }
The scripts (beer.py, vodka.py and whisky.py) print their versions in format like: "/home/beer.py 1.0.0". And i need to get this versions, compare it with versions that i store in database (this is the actual versions) and if versions are not equal then copy an actual version from svn (svn paths also stored in database) to the server. How can i do that using ansible capabilities?
Ansible does not have a module to directly check the versions of any program. You have two options, both involving a bash command to extract the version number from the output of your scripts. This should probably do:
Option 1: Run tasks to fetch the version. Basically what you already had plus the version extraction.
Now you have a variable
versions
registered and inversions.result
is a list of dicts which contain the sw_path and the return value of each loop item.Something like this:
To see the real content of the registered data use a debug task like so:
Option 2: Use custom facts
You can install a script on the remote hosts (with Ansible of course) which returns the versions. When Ansible connects to these hosts it will automatically run these scripts and use them as facts, just like any other system property.
Here's the docs for local facts.
The script could be as simple as this:
The output should be looking like this:
You can install this script with Ansible, for example with the template module. So you can even make it dynamic based on your list of
sw_path
items.After installation you need to reload the facts. You can do this with this task right after your template task:
Now you will be able to directly access the versions as
ansible_local.versions.beer
etc.So much for detecting the versions.
You did not mention it but I assume you do know how to get the version from your database for comparison. Otherwise you would need to provide a lot more data. So let's assume you have the "should versions" stored as
should["beer"]
,should["vodka"]
andshould["whiskey"]
.You now can compare the versions with the the version_compare filter.
This would only upgrade but never downgrade in case a newer version than referenced in your database is installed. Of course you can directly compare the strings and make sure you always install the exact version.