HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Ansible Playbook Localhost Invocation Working Directory

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
directorylocalhostworkinginvocationplaybookansible

Problem

I want to run an ansible playbook locally (host is localhost, like -i "localhost,") and inside the Playbook obtain the working directory from which the ansible-playbook command was invoked.

However, when I read out the current working directory in a playbook task, I get the directory of the playbook itself.

Is there any variable I can refer to to obtain the working directory on the controlling host (which is localhost) in my case?

I can inject via a variable -e but I would prefer to have a fall-back that if that variable is not set I can obtain it from the ([local]host) environment.

Solution

You can use env lookup plugins. Plugins are always evaluated in the context of "parent" ansible process on the control host.

- debug:
    msg: "{{ lookup('env', 'PWD') }}"


More lookup plugins can be found here: http://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html

Code Snippets

- debug:
    msg: "{{ lookup('env', 'PWD') }}"

Context

StackExchange DevOps Q#3763, answer score: 17

Revisions (0)

No revisions yet.