ansible shell/command module not executing script at remote machine - shell

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

Related

I'm trying to install maven 3.8.6 to using a playbook. the playbook is successfully executed but the maven version not updated

hosts: client
become: true
tasks:
name: Download Apache Maven
get_url: url=https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz dest=/opt
name: untar maven
command: tar xvf /opt/apache-maven-3.8.6-bin.tar.gz -C /opt
name: Move to a smaller directory
command: mv /opt/apache-maven-3.8.6 /opt/maven
name: file created
file: path=/etc/profile.d/maven.sh state=touch
name: copy content
copy: content="export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}" dest=/etc/profile.d/maven.sh
name: source profile
command: sh /etc/profile.d/maven.sh

How can I move the centos user's homedir using ansible?

I've got a CentOS cluster where /home is going to get mounted over nfs. So I think the centos user's home should get moved to somewhere which will remain local, maybe /var/lib/centos or something. But given centos is the ansible_user I can't use:
hosts: cluster
become: yes
tasks:
- ansible.builtin.user:
name: centos
move_home: yes
home: "/var/lib/centos"
as unsurprisingly it fails with
usermod: user centos is currently used by process 45028
Any semi-tidy workarounds for this, or better ideas?
I don't think you're going to be able to do this with the user module if you're connecting as the centos user. However, if you handle the individual steps yourself it should work:
---
- hosts: centos
gather_facts: false
become: true
tasks:
- command: rsync -a /home/centos/ /var/lib/centos/
- command: sed -i 's,/home/centos,/var/lib/centos,' /etc/passwd
args:
warn: false
- meta: reset_connection
- command: rm -rf /home/centos
args:
warn: false
This relocates the home directory, updates /etc/passwd, and then removes the old home directory. The reset_connection is in there to force a new ssh connection: without that, Ansible will be unhappy when you remove the home directory.
In practice, you'd want to add some logic to the above playbook to make it idempotent.

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.

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

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