FYI: What is PWD when executed as host:localhost vs delegate_to vs local_action ?

Thought I’d share some gory details I just worked through. I was writing a self-check playbook and wanted to look at files in the playbook directory. Note that a module would probably solve my problem more effectively than PB code but here’s what I found…

  1. vars.playbook_dir=‘.’ - as you’ll see this doesn’t necessarily take you to the right location
  2. vars.inventory_dir= - this is better but only if inventory_dir=playbook_dir

So my attempts to use #1 went south until I discovered the following variations. As stated in the documentation, local_action is an alias for delegate_to: localhost or 127.0.0.1. What’s not stated is that “delegate_to localhost” is treated as a special case. delegate_to any-other-server will work the same as hosts: any-other-server

  • hosts: localhost
    ssh_user: hi_there
    tasks:

  • name: pwd_localhost
    command: pwd => /home/hi_there
    register: pwd_localhost

  • name: pwd_delegate_dns

command: pwd => /home/hi_there
delegate_to: dns_of_localhost
register: pwd_delegate_dns

  • name: pwd_delegate_localhost

command: pwd =>
delegate_to: localhost
register: pwd_delegate_localhost

  • name: pwd_local_action
    local_action: command pwd =>
    register: pwd_local_action

  • name: pwd_127

command: pwd =>
delegate_to: 127.0.0.1
register: pwd_127

No offense intended, but care to relate what you found in words and what you mean by “a special case” ?

I also don’t know what you mean by “vars.playbook_dir” with the dot notation, which is difficult because I wrote the program :slight_smile: