0

I have a shell script which just changes directory (here directory path is given).

shell file

#!/bin/bash
p="dir/file/create"
cd "$p"
exec bash

Ansible playbook

---
 - hosts: localhost
   gather_facts: true
   become: true
   become_user: oracle
   become_flags: 'content-ansible'
   tasks:
   - name: changing dir now..
     command: sh /dir/file/create/vars.sh

I want to run a shell script to change directory path in ANSIBLE and run subsequent shell file(#2) in the directory (again shell script).

Ansible playbook completes but I never am able to get into a directory and execute the shell script(#2).

3 Answers 3

4

Why don't you use the chdir parameter?

chdir: cd into this directory before running the command

From the documentation:

- name: Change the working directory to somedir/ before executing the command.
  command: somescript.sh
  args:
    chdir: somedir/
0

In your script you should enter complete PATH:

#!/bin/bash
p="/dir/file/create"
cd "$p" 

Don't use sh in front of COMMAND module:

   - name: changing dir now..
     command: /dir/file/create/vars.sh  

but 'shell' module is better than 'command' module for your issue.

-2

I tried this option and it seems fine for me:

pre_tasks:
 - include_vars: vars.yml
tasks:
 - name: Do task...
   shell: cd "{{v_dir}}" && sh shell2.sh
2
  • The chdir parameter is also available for the shell module.
    – kaliko
    Apr 25, 2019 at 15:02
  • Kind of a redundant hack. Technically it may work, but its not too far off from writing everything into a bash script. Ansible has the tools you need to complete this, as @kaliko has pointed out.
    – 0xSheepdog
    Apr 29, 2019 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .