Run shell command from Ansible

8,333

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/
Share:
8,333

Related videos on Youtube

shashank barki
Author by

shashank barki

Updated on September 18, 2022

Comments

  • shashank barki
    shashank barki over 1 year

    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).

  • kaliko
    kaliko about 5 years
    The chdir parameter is also available for the shell module.