Ansible execution using variables - ansible

hi I want to execute "java -jar xxxxxxx.war 172.xx.xxx.xx" command on a host.
I am able to execute in a single system through Ansible, but I want to execute that command in a multiple hosts and that execution needs to be done in their respective system IP's.
eg:
java -jar xxxxxxx.war 172.xx.xxx.01 needs to be executed on the system with the IP 172.xx.xxx.01
java -jar xxxxxxx.war 172.xx.xxx.02 needs to be executed on the system with the IP 172.xx.xxx.02
- name: Executing the jar file
win_shell: "CMD /C \"java -jar generateKeyStoreFiles.jar storeipTest.csv\""
args:
chdir: C:/Keystore_Pem_FileAutoGenerationOracle_allgoodthings/
- name: Copying of Xstore POS Installer
win_copy:
src: /root/Installers/Softwares/Xstore
dest: C:/Installers/Softwares/
- name: Copying of xstore_mobile.keystore file
win_copy:
src: C:/Keystore_Pem_FileAutoGenerationOracle_allgoodthings/finalcerts/cert/xstore_mobile.keystore
dest: C:/Installers/Softwares/Xstore/
remote_src: yes
- name: install Xstore POS
win_shell: "CMD /C \"java -jar xstore-18.0.0.0.654-0.0.0-0.0-XST-pos-install.jar\""
args:
chdir: C:/Installers/Softwares/Xstore/
how is that possible ?, I need to execute that on windows hosts.

I guess you could create an inventory and a playbook to achieve that.
inventory
[all]
13.48.139.217
13.48.139.218
playbook.yml
- hosts: all
tasks:
- name: run java command
command: java -jar xxxxxxx.war {{ansible_host}}
{{ansible_host}} is an Asnsible variable that will be replaced with an IP address mentioned in inventory.
Then execute it ansible-playbook -i inventory playbook.yml.

Related

Ansible- changing directory for all tasks

I have a script that I want to write as a playbook.
first I should go to my specific directory and then run some tsm commands for backup and cleanup.
I tried to change directory which works fine. but the tasks after that("list files") is running in my home directory.
How can I change the working directory for all tasks?
Thanks for your help!
- name: "Change Directory"
win_shell: cd /d E:\mydirectory
args:
executable: cmd
- name: list files
win_shell: dir
args:
executable: cmd
register: myfiles
Each task is independent, so you have to change the working directory on each win_shell invocation. One way to do this is to use module_defaults on a block:
- module_defaults:
win_shell:
executable: cmd
chdir: 'E:\mydirectory'
block:
- name: list files
win_shell: dir
register: myfiles

Ansible, copy script, then execute in remote machine

I want to copy a script to a remote server and then execute it. I can copy it to the directory /home/user/scripts, but when I run ansible script, it returns Could not find or access '/home/user/scripts/servicios.sh'
The full error output is:
fatal: [192.168.1.142]: FAILED! => {"changed": false, "msg": "Could not find or access '/home/user/scripts/servicios.sh'"}
Here is the ansible playbook
- name: correr script
hosts: all
tasks:
- name: crear carpeta de scripts
file:
path: /home/user/scripts
state: directory
- name: copiar el script
copy:
src: /home/local/servicios.sh
dest: /home/user/scripts/servicios.sh
- name: ejecutar script como sudo
become: yes
script: /home/user/scripts/servicios.sh
You don’t need to create a directory and copy the script to target (remote node), the script module does that for you. It takes the script name followed by a list of space-delimited arguments. The local script at path will be transferred to the remote node and then executed. The script will be processed through the shell environment on the remote node. You were getting the error because script module expects the path /home/user/scripts/servicios.sh on your Ansible controller (the node where you are running the playbook from). To make it work you can specify correct path (/home/local/servicios.sh) in script task instead of /home/user/scripts/servicios.sh which is the path on the remote node. So you can change the playbook like this: You can also register the result of that command as a variable if you would like to see that.
---
- name: correr script
hosts: all
become: yes
tasks:
- name: ejecutar script como sudo
script: /home/local/servicios.sh
register: console
- debug: msg="{{ console.stdout }}"
- debug: msg="{{ console.stderr }}"
What if don’t want to go for script module and you are interested in creating a directory and copy the script to target (remote node) explicitly, and run it? No worries, you can still use the command module like this:
---
- name: correr script
hosts: all
become: yes
tasks:
- name: crear carpeta de scripts
file:
path: /home/user/scripts
state: directory
- name: copiar el script
copy:
src: /home/local/servicios.sh
dest: /home/user/scripts/servicios.sh
- name: ejecutar script como sudo
command: bash /home/user/scripts/servicios.sh
register: console
- debug: msg="{{ console.stdout }}"
- debug: msg="{{ console.stderr }}"
But I strongly recommend to go for script module.
The script tag itself transfers the script from the local machine to remote machine and executes it there.
So, the path specified in the script module is of the local machine and not the remote machine i.e., /home/local/servicios.sh instead of /home/user/scripts/servicios.sh
As you have specified the path which is supposed to be on the remote machine, ansible is unable to find that script on local machine at the given path which results in the given error.
Hence, update the path in the task to local path as shown below,
- name: ejecutar script como sudo
become: yes
script: /home/local/servicios.sh
So scripts cant be executed inside the remote server and should be
executed via local machine to the remote?
#thrash3d No, it is not like that. When you use script tag the script is transferred to the remote machine and then it is executed there. If there is a script which you don't want to put on your remote machine and just want to execute it then you can use script tag.
If you want that script on your remote machine then you can first copy your script on remote machine and then execute it there.
Both ways are correct and it is up to you which case suits you better.

how to install the script interactive via ansible playbook

I can run the script with the command line argument on the linux server it works fine.
for e.g.: ./install.sh -n -I <IP address of the server>
The above command is able to install the script on the server.
When I am trying to do via ansible (version 2.5) playbook using the shell module it gives me an argument error.
- name: Running the script
shell: yes | ./fullinstall
Expect modules has been tried.
--my-arg1=IP address
- shell: "./install.sh -n -I"
args:
chdir: somedir/
creates: somelog.txt
You can look here for examples.
You can also place the install.sh file on the server as a template. Then you can set the variables as desired in Jinja2.
- name: Template install.sh
template:
src: /install.sh.j2
dest: /tmp/install.sh
- shell: "cd /tmp/ ; ./install.sh
Your install.sh.j2 contains:
IP adres: {{ my_ip }}
And set the variable on the command line with:
ansible-playbook -e my_ip="192.168.0.1"
Use command module
- name: run script
command: /path/to/install.sh -n -I {{ ip_addrress }}
playbook
ansible-playbook -e ip_address="192.168.3.9" play.yml
If you want to interactively wanted to enter the IP address, use prompt module.

File created by ansible script module not present after running against remote node

I'm setting up some server in AWS, and want to use Ansible to do some shell in remote nodes. I write playbook as follow
- hosts: remote-nodes
tasks:
- name: Execute script
script: /home/ubuntu/FastBFT_ethereum/experiment/a.sh
remote nodes a.sh as follow
#!/usr/bin/env bash
echo "test">> test.txt
python writejson.py
But when I check the test.text, I find it doesn't work in remote nodes.help me please.
Assuming that you want test.txt to be created in the experiment directory, this should be changed to something like:
- hosts: remote-nodes
tasks:
- name: Execute script
script: /home/ubuntu/FastBFT_ethereum/experiment/a.sh
args:
chdir: /home/ubuntu/FastBFT_ethereum/experiment

Unable to run bashrc file using ansible

Unable to run bashrc file using ansible.
- name: Source Bashrc
action: shell source /local/apps/actional/.bash_profile
is not working.
source is a build-in, not command.
Try this:
---
- hosts: target-server
gather_facts: no
tasks:
- copy:
content: export MYVAR=123
dest: /tmp/source_test
- shell: /bin/bash -c 'source /tmp/source_test; echo $MYVAR'
Keep in mind that you can use sourced environment only within one Ansible task – each task is executed in new shell.

Resources