How to install -t? - ansible

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.

Related

Install rpm after copy, with ansible

I have an ansible playbook which will copy a file into a location on a remote server. It works fine. In this case, the file is an rpm. This is the way it works:
---
- hosts: my_host
tasks:
- name: mkdir /tmp/RPMS
file: path=/tmp/RPMS state=directory
- name: copy RPMs to /tmp/RPMS
copy:
src: "{{ item }}"
dest: /tmp/RPMS
mode: 0755
with_items:
[any_rpm-x86_64.rpm]
register: rpms_copied
Now, with the file successfully on the remote server, I need to start some new logic that will install the rpm that sits in /tmp/RPMS. I have run many different versions of the below (So this code is added onto the above block):
- name: install rpm from file
yum:
name: /tmp/RPMS/any_rpm-x86_64.rpm
state: present
become: true
I don't know if the formatting is incorrect, or if this is not the way. Can anyone advise as to how I can get the rpm in the directory /tmp/RPMS installed using a new few lines in the existing playbook?
Thanks.
I did not find this anywhere else, and it genuinely took me all of my working day to get to this point. For anyone else struggling:
- name: Install my package from a file on server
shell: rpm -ivh /tmp/RPMS/*.rpm
async: 1800
poll: 0
become_method: sudo
become: yes
become_user: root

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

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.

Compiling and installing a simple binary using Ansible

Say I have a very simple program magic_command.c that I want compiled to magic_command and installed in /usr/local/bin.
One method I can think of for doing this is roughly the following with Ansible:
Create a temporary directory on the remote host
Copy the magic_command.c and a Makefile to the temporary directory
Run make all to create magic_command
Run make install, a target that copies magic_command to /usr/local/bin
Is there a simpler or more concise way of doing this with Ansible?
I ended up using a method very similar to that described in my original question:
- name: Create temporary directory for compilation
command: mktemp -d /tmp/magic_command.XXXXXXXXX
register: magic_command_temp_dir
- name: Copy source and makefile
copy: src={{ item }} dest={{ magic_command_temp_dir.stdout }}
with_items:
- magic_command.c
- Makefile
- name: Compile executable
shell: make chdir={{ magic_command_temp_dir.stdout }}
- name: Install executable
copy: remote_src=True src={{ magic_command_temp_dir.stdout }}/magic_command dest=/usr/local/bin/magic_command mode=0755 owner=root group=root
- name: Remove temporary directory
file: name={{ magic_command_temp_dir.stdout }} state=absent

template task: write to root owned directory

I want to copy a template generated file to /etc/init.d folder. But template task doesn't seem to support sudo parameter.
What is the recommended way to handle this? should I copy it to temporary directory and then move file with with sudo?
The playbook task looks like as shown below. Ansible version 1.8.2
- name: copy init script
template: src=template/optimus_api_service.sh dest=/etc/init.d/optimus-api mode=0755 force=yes owner=root group=root
I have tested the following playbook and it works.
My setup:
The User vagrant on the machine vm is allowed to execute commands password-free with sudo.
I created a simple template and installed it with the following playbook:
---
- name: Test template
hosts: vm
gather_facts: no
remote_user: vagrant
vars:
bla: blub # some variable used in the template
tasks:
- name: copy init script
sudo: yes # << you have to activate sudo
sudo_user: root # << and provide the user
template: src=template/test.j2 dest=/opt/test mode=0755 force=yes owner=root group=root

Resources