How can I run a command only if an online file has changed in ansible - 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.

Related

Ansible: 'shell' not executing

I'm trying to run a shell command to install a package downloaded from my local Artifactory repository, as I don't have access to download it straight from the internet.
When I run the command directly on the node as such
rpm -ivh kubectl-1.1.1.x86_64.rpm --nodigest --nofiledigest
It works perfectly.
But then put in Ansible playbook as such
- name: Install Kubectl
shell: rpm -ivh kubectl-1.1.1.x86_64.rpm --nodigest --nofiledigest
Nothing happens.
It doesn't error.. It just doesn't install.
I've tried the command and ansible.builtin.shell module as well, but nothing works.
Is there a way to do this please?
There are different topics in your question.
Regarding
to install a package downloaded from my local Artifactory repository, as I don't have access to download it straight from the internet.
you can use different approaches.
1. Direct download
- name: Make sure package becomes installed from internal repository
yum:
name: https://{{ REPOSITORY_URL }}/artifactory/kube/kubectl-{{ KUBE_VERSION }}.x86_64.rpm
state: present
2. Configure local repository
The next one is to provide a .repo template file like
[KUBE]
name = Kubectl - $basearch
baseurl = https://{{ REPOSITORY_URL }}/artifactory/kube/
username = {{ API_USER }}
password = {{ API_KEY }}
sslverify = 1
enabled = 1
gpgcheck = 1
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-KUBE
and to perform
- name: Make sure package becomes installed from internal repository
yum:
name: kubectl
state: present
This is possible because JFrog Artifactory can provide local RPM repositories if configured correctly. For more information you research the documentation there since it is almost only about proper configuration.
Regarding
Nothing happens. It doesn't error.. It just doesn't install.
you can use several task to split up your steps, make them idempotent and get an better insight how they are working.
3. shell, rpm and debug
- name: Make sure destination folder for package download (/opt/packages) exists
file:
path: "/opt/packages/"
state: directory
- name: Download RPM to remote hosts
get_url:
url: "https://{{ REPOSTORY_URL }}/artifactory/kube/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
dest: "/opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
- name: Check package content
shell:
cmd: "rpm -qlp /opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
register: rpm_qlp
- name: STDOUT rpm_qlp
debug:
msg: "{{ rpm_qlp.stdout.split('\n')[:-1] }}"
- name: Install RPM using 'command: rpm -ivh'
shell:
cmd: "rpm -ivh /opt/packages/kubectl-{{ KUBE_VERSION }}.x86_64.rpm"
register: rpm_ivh
- name: STDOUT rpm_ivh
debug:
msg: "{{ rpm_ivh.stdout.split('\n')[:-1] }}"
Depending on the RPM package, environment and configuration, all may just work good.
Try use command module and register the output i use it to install oci8 by pecl for oracle database on linux

Packer Ansible provisioner is skipping tasks in Ansible playbook

I am using the Packer Ansible provisioner in a JSON file to download a zip file from the web that contains a program and have that program run at startup by editing the Windows Registry using the win_regedit Ansible module:
---
- hosts: default
tasks:
- name: Create Bginfo Directory
win_file:
path: C:\Bginfo
state: directory
- name: Download BgInfo64.zip
win_get_url:
url: https://download.sysinternals.com/files/BGInfo.zip
dest: C:\Bginfo\BgInfo64.zip
- name: Unzip BgInfo64.zip
win_unzip:
src: C:\Bginfo\BgInfo64.zip
dest: C:\Bginfo
delete_archive: true
- name: Run Bginfo at startup
win_regedit:
key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
name: Bginfo
data: C:\Bginfo\Bginfo64.exe C:\BgInfo\default.bgi /timer:0 /nolicprompt
type: string
When I check the machine, the registry edits were not made and the archive is not deleted after un-zipping.
As per the logs, win_regedit is using module file Using module file /opt/ansible/ansible/lib/ansible/modules/windows/win_regedit.ps1
win_unzip is using /opt/ansible/ansible/lib/ansible/modules/windows/win_unzip.ps1
I looked up the PowerShell files online and everything I'm doing seems legal by Ansible standards so I'm unsure why those tasks are not completed.

How to only download an appimage if the version doesn't match the existing in 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

How to install -t?

I have the following statement, that I would like to translate into Ansible:
$ curl -L https://github.com/drone/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz | tar zx
$ sudo install -t /usr/local/bin drone
How to build an Ansible task for it?
I've tried:
- name: Extract droner exec runner into /usr/local/bin
unarchive:
src: drone_linux_amd64.tar.gz
dest: /usr/local/bin/
mode: "0664"
But it does not work as expected.
install command manages the permissions of the file, making sure the execute permissions are added to the file during installation.
unarchive just extracts the contents of the archive file to the destination. Set the desired mode to the task, in this case to make it executable add the +x permission.
- name: Extract droner exec runner into /usr/local/bin
unarchive:
src: drone_linux_amd64.tar.gz
dest: /usr/local/bin/
mode: "a+x"
a+x, giving execute permission for all expecting the drone command will be used across users.
- hosts: localhost
become: yes
gather_facts: no
tasks:
- name: Get the file from github
get_url:
url: "https://github.com/drone/drone-cli/releases/latest/download/drone_linux_amd64.tar.gz"
dest: /tmp/drone_linux_amd64.tar.gz
- name: Unarchive the file
unarchive:
src: /tmp/drone_linux_amd64.tar.gz
dest: /tmp
mode: 0664
- name: Install
shell:
cmd: install -t /usr/local/bin drone
chdir: /tmp
Task 1: Fetching the file form the github with get_url. you can also you shell module. get_url module
Task 2: Unarchive the file using unarchive module. unarchive module
Task 3: Install with shell command shell module
I have unarchive it in the tmp folder. and then installed it.

ansible shell/command module not executing script at remote machine

While trying to run startup.sh script for Tomcat-8, I am getting below error. Playbook is executing successfully with STDOUT- "Tomcat started" but it is not reflecting in the remote machine.
The error is there for both modules - command and shell
- name: Download Tomcat version 8
get_url:
url: http://mirrors.wuchna.com/apachemirror/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
dest: /tmp/apache-tomcat-8.5.35.tar.gz
- name: Untar Tomcat to /opt
shell: tar -zxvf apache-tomcat-8.5.35.tar.gz -C /opt
args:
chdir: /tmp/
warn: False
- name: Change permissions of Tomcat folder
file:
path: /opt/apache-tomcat-8.5.35
mode: 0777
recurse: yes
- name: Set Catalina Home
lineinfile:
path: /etc/profile.d/maven.sh
regexp: '^#?\s*export CATALINA_HOME=(.*)$'
line: 'export CATALINA_HOME=/opt/apache-tomcat-8.5.35'
state: present
- name: execute source
shell: source maven.sh
args:
chdir: /etc/profile.d/
executable: /bin/sh
- name: start catalina
become: true
command: /opt/apache-tomcat-8.5.35/bin/startup.sh &>> /var/log/log.txt
Tried running your tasks in a playbook on Centos7 and it executes successfully with Tomcat up and healthy. (Had to install maven first)
I might need more info to debug this.
Q: Are you planning to run tomcat as root user? (if you are.. you
shouldn't)
Q: What is the OS on remote?
Q: Have you installed JRE or
JDK properly ?
Have a look at this example: https://github.com/ansible/ansible-examples/tree/master/tomcat-standalone

Resources