Ansible- changing directory for all tasks - ansible

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

Related

Change current directory and stay there [duplicate]

- name: Go to the folder
command: chdir=/opt/tools/temp
When I run my playbook, I get:
TASK: [Go to the folder] *****************************
failed: [host] => {"failed": true, "rc": 256}
msg: no command given
Any help is much appreciated.
There's no concept of current directory in Ansible. You can specify current directory for specific task, like you did in your playbook. The only missing part was the actual command to execute. Try this:
- name: Go to the folder and execute command
command: chdir=/opt/tools/temp ls
This question was in the results for when I was trying to figure out why 'shell' was not respecting my chdir entries when I had to revert to Ansible 1.9. So I will be posting my solution.
I had
- name: task name
shell:
cmd: touch foobar
creates: foobar
chdir: /usr/lib/foobar
It worked with Ansible > 2, but for 1.9 I had to change it to.
- name: task name
shell: touch foobar
args:
creates: foobar
chdir: /usr/lib/foobar
Just wanted to share.
If you need a login console (like for bundler), then you have to do the command like this.
command: bash -lc "cd /path/to/folder && bundle install"
You can change into a directory before running a command with ansible with chdir.
Here's an example I just setup:
- name: Run a pipenv install
environment:
LANG: "en_GB.UTF-8"
command: "pipenv install --dev"
args:
chdir: "{{ dir }}/proj"

Ansible become user do not run /etc/profile

I have playbook that installs shell scripts into /etc/profile.d/app.sh which appends my application start and stop scripts to $PATH, These 2 below steps runs as root user
- name: add binaries to PATH
copy:
args:
src: app.sh
dest: /etc/profile.d/
owner: root
group: root
mode: 0644
Then I am sourcing /etc/profile to reflect the changes
- name: reload profile
shell: source /etc/profile
After this I need to switch to my application user "appuser" to start the process
- name: start the application
shell: startMyapp application
become: yes
become_user: appuser
But I get error startMyapp: command not found, looks like when I use become_user /etc/profile is not getting executed to get the updated $PATH.
How can I make changes in my playbook that source /etc/profile when I use become_user?

Ansible execution using variables

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.

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.

Ansible: How to change active directory in Ansible Playbook?

- name: Go to the folder
command: chdir=/opt/tools/temp
When I run my playbook, I get:
TASK: [Go to the folder] *****************************
failed: [host] => {"failed": true, "rc": 256}
msg: no command given
Any help is much appreciated.
There's no concept of current directory in Ansible. You can specify current directory for specific task, like you did in your playbook. The only missing part was the actual command to execute. Try this:
- name: Go to the folder and execute command
command: chdir=/opt/tools/temp ls
This question was in the results for when I was trying to figure out why 'shell' was not respecting my chdir entries when I had to revert to Ansible 1.9. So I will be posting my solution.
I had
- name: task name
shell:
cmd: touch foobar
creates: foobar
chdir: /usr/lib/foobar
It worked with Ansible > 2, but for 1.9 I had to change it to.
- name: task name
shell: touch foobar
args:
creates: foobar
chdir: /usr/lib/foobar
Just wanted to share.
If you need a login console (like for bundler), then you have to do the command like this.
command: bash -lc "cd /path/to/folder && bundle install"
You can change into a directory before running a command with ansible with chdir.
Here's an example I just setup:
- name: Run a pipenv install
environment:
LANG: "en_GB.UTF-8"
command: "pipenv install --dev"
args:
chdir: "{{ dir }}/proj"

Resources