How to only download an appimage if the version doesn't match the existing in ansible - ansible

I want to download the latest appimage of bitwarden, but only if the downloadable version is newer than the local one.
- name: Download and place Bitwarden Appimage
get_url:
url: "https://vault.bitwarden.com/download/?app=desktop&platform=linux"
follow: yes
dest: /home/user
register: foo
when: foo !== /home/user/Bitwarden-{1....9}-x86_64.AppImage
Is it possible to do this within the same task, or do I have to split it up over 2? Also is the regex syntax correct? The current version is: 1.25.1

Related

Ansible Perform Tasks in localhost on Multiple Hosts

I'm planning an Ansible playbook which will query version number of specific .exe file on windows hosts, then generate diff version between the version installed on the Windows hosts and the latest version stored on the Ansible controller Linux machine ("localhost"), and deploy the diff version to the Windows hosts.
My playbook looks something like:
- hosts: winClients
gather_facts: False
tasks:
- name: check exe file version
win_file_version:
path: C:\my.exe
register: exe_file_version
- name: Set client version
set_fact:
winClientExeVersion: "{{ exe_file_version.win_file_version.file_version }}"
Then, on localhost, I have folder for each version, and I'd like to generate the diff between the latest version and the version on the winClients. The versions are stored on localhost in folders named with the version numbers, e.g. MyVersions/1.0.0.0/abc.exe, abc.dll, aaa.txt, ... and MyVersions/1.0.0.1/abc.exe, abc.dll, aaa.txt, ... etc. And I have special folder, MyVersions/LatestVersion/ that always contains the latest version. So I need something like:
- hosts: localhost
gather_facts: False
tasks:
- name: check latest version on server using PEV
shell: peres -v /home/user/MyVersions/LatestVersion/My.exe | awk '{print $3}'
register: latest_file_version
- name: Set server version
set_fact:
serverLatestVersion: "{{ latest_file_version.stdout }}"
- name: If versions differ, generate diff between client and latest versions to temp DiffFolder, then delete empty folders
shell: rsync -rvcm --compare-dest=../{{hostvars.winClients.winClientExeVersion}} . ../DiffFolder && find ../DiffFolder -depth -type d -empty -delete
args:
chdir: /home/user/MyVersions/LatestVersion
when: serverLatestVersion != hostvars.winClients.winClientExeVersion
Then I copy the generated diff to the Windows clients using win_copy.
Now, all this works OK when winClients represents only one specific client.
My problem is, how to do it for group of clients, i.e. in case when winClients represents a group instead of one specific computer? How can I generate diff for each client separately (and each client might have different version installed on it), based on its previously-retrieved version number, when its version number differs from the latest version on server?
The winClients upper section will assign version number for each of the clients, the problem is with the localhost tasks and their when condition.
So it turns out to be pretty simple: with_items does the trick, and it can be referred later in when as well.
Example:
- hosts: localhost
gather_facts: False
tasks:
- name: Notify if versions differ
debug:
msg: "client and server versions differ: Server: {{ serverLatestVersion}}, Client: {{ hostvars[item]['winClientExeVersion'] }}"
with_items: "{{ groups['clients'] }}"
when: serverLatestVersion != hostvars[item]['winClientExeVersion']
This will loop through the clients in clients group, and for each client that got a different version than the server, notify in debug.
Similar method can be used for each task (copy, rsync, etc.) that requires to be executed only against relevant client, based on when condition.

Ansible yum disablerepo does not work if repo is not installed

I have a number of different Centos7 servers running. I like to use ansible to update them all at once.
As one of my servers has an additional repository enabled, which I do not want to update. I've added to the playbook the option to disable this repo. This works as expected.
However, on my other servers, I did not install and enable this repo. When using the disablerepo in my ansible playbook, I get an error: repository not found.
How do I solve this in the ansible-playbook? Is it possible to add an condition like, if repo installed; then disablerepo; else do nothing?
Is it possible to ignore these errors?
ansible-playbook:
---
- hosts: [all]
tasks:
- name: update all packages to lastest version
yum:
name: '*'
state: latest
disablerepo: sernet-samba-4.2
you can put ignore_errors: yes as in the link from the comment, or you can put when, only if certain package is installed, sure you have to register them to variables first, I'm thinking something like:
- name: check if installed
shell: rpm -qa sernet-samba-4.2
register: is_installed
- name: update all packages to lastest version
yum:
name: '*'
state: latest
disablerepo: sernet-samba-4.2
when: is_installed.rc == 1
Warning: Untested.
After a day of research in internet and experiments finally found a solution that worked. Try to use wildcard.. then it will not fail when repo is missing.
yum:
name: ''
state: latest
disablerepo: sernet-samba

Can ansible get a url from an anchor on a webpage?

I want Ansible to install a .deb package from a url when the url changes every week (because of updates). There is an anchor to the dynamic download url on a static download page. Is there a trick to have ansible figure out the url?
E.g. on a certain page, there is a link called "Latest version" as so:
Latest version.
Can I get ansible to get URL_TO_DOWNLOAD_DEB?
Here is some made-up trick that hopefully illustrates what I mean. Obviously, this does not work.
- name: Find link to DEB package
regex_from_url:
url: http://some.download.page
regex: g/<a href="([^"]+)">Latest version<\/a>./
register: URL_TO_DOWNLOAD_DEB
- name: Install DEB package
apt:
deb: {{ URL_TO_DOWNLOAD_DEB }}
You can try something like this:
- name: Send GET request to target
shell: wget -O - http://some.download.page | grep '>Latest version<' | sed -n 's/.*href="\(.*\)".*/\1/p'
register: web
args:
warn: False
- name: Show download link
debug:
msg: "{{ web.stdout }}"

Joomla - Get Latest Version Zip Link - scrip Install

Does Joomla have a Link to the Latest Version? Wordpress for example has a link to https://wordpress.org/latest.zip , so I know I can always download that zip and get the latest version.
I am working on ansible script to automatically setup a server and install joomla and I need to keep it simple so I would like to spesify a link or api call or something to always get the latest joomla version zip.
Any help appreciated.
David
good question - it is certainly not as easy as wordpress.
You can download the latest version using ansible in the following way:
Download the XML file you mentioned to your local machine:
- name: download latest joomla packages list to local machine
tags: joomlanew
get_url:
url: https://update.joomla.org/core/sts/extension_sts.xml
dest: "{{ role_path }}/scripts/extension_sts.xml"
force: yes
delegate_to: 127.0.0.1
Run a python script to parse this XML to find the latest package zip file:
- name: find latest joomla version
tags: joomlanew
command: python ./latest_joomla.py
args:
chdir: "{{ role_path }}/scripts"
delegate_to: 127.0.0.1
register: xmlresp
You can then use {{ xml.resp.stdout }} in ansible as the input for your download code:
- name: download & unzip joomla
tags: joomlanew
unarchive:
src: "{{ xmlresp.stdout }}"
dest: /home/username/public_html/
remote_src: yes
mode: 0755
FYI the code in th python script to parse the XML would be:
from xml.dom import minidom
xmldoc = minidom.parse('extension_sts.xml')
itemlist = xmldoc.getElementsByTagName('downloadurl')
last=(len(itemlist))-1
print (itemlist[last].firstChild.data)
Ok after some digging I guess the best way to go after this is the way Joomla Core does it:
Get detailsurl out of Last entry out of the Joomla Core Update site (https://update.joomla.org/core/list.xml) - in this instance https://update.joomla.org/core/sts/extension_sts.xml
Get downloadurl out of Last entry out of the url you got in step 1 - in this instance - you need to make sure this is not a PATCH url but UPDATE
You can use this link to download the latest version: https://github.com/joomla/joomla-cms/archive/staging.zip

How can I run a command only if an online file has changed in ansible

I have the following task in my ansible playbook:
- name: download last buildtools
get_url:
url: https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
dest: ~/buildtools/BuildTools.jar
I want the following command to be run only if the BuildTools.jar file has changed:
java -jar BuildTools.jar --rev latest
How can I do this in my ansible playbook ?
EDIT: Just to be clear, I want the command to be triggered only if the version of the file at the url above has changed. So, in the case, each time a new version of minecraft is supported by the buildtools.
Finally, I found an elegant way to do what I wanted:
From the doc of the force parameter in the get_url module,
If yes and dest is not a directory, will download the file every time and replace the file if the contents change.
So my config is now:
- name: Download the last buildtools
get_url:
url: https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
dest: ~/buildtools/BuildTools.jar
force: yes
register: buildtools
- name: Update BuildTools
command: java -jar BuildTools.jar --rev latest
args:
chdir: ~/buildtools/
when: buildtools.changed
So the Update BuildTools task is only run if the file downloaded at the url is different of the original file on the server, or if it is the first time that the file is downloaded.

Resources