Variable file path in lookup

4,811 views
Skip to first unread message

Chris Carter

unread,
Oct 15, 2013, 5:45:39 PM10/15/13
to ansible...@googlegroups.com
Hi there,

I'm attempting to configure a variable lookup to a public key file, and am running into some trouble.  What confuses me is that the variable substitution works fine in a debug statement, but then blows up in the lookup statement for assigning authorized key values.  Here's the general idea;

## provisioning/git.yml (playbook):
---
- hosts: git
  sudo: true
  roles:
    - { role: git, git_users: [ 'me' ] }

## provisioning/roles/git/tasks/main.yml:
---
- user: name=git state=present home=/opt/git
- debug: msg={{ '../../users/files/' + item + '.pub' }}
  with_items: git_users
- authorized_key: user=git key={{ lookup('file', '../../users/files/' + item + '.pub') }}
  with_items: git_users

When I run ansible against the git.yml playbook, I end up with the following:

TASK: [debug msg=../../users/files/.pub] **************************************
ok: [dev-build-c.oedev.us] => (item=me) => {"item": "me", "msg": "../../users/files/me.pub"}
ERROR: blah/blah/blah/provisioning/roles/users/files/.pub does not exist

I'm really struggling with understanding what I am missing in terms of looping and the lookup functionality.  We're using lookups elsewhere with dynamically constructed paths for passwords, and it seems to work like I'd expect.  But when I introduce a loop, it falls apart.

Thanks for any pointers to relieve me of my ignorance!

Chris

Michael DeHaan

unread,
Oct 16, 2013, 3:55:34 PM10/16/13
to ansible...@googlegroups.com
I don't see this as blowing up, I see this as reporting an error that it couldn't find the file.

Help me understand why the file is not where you think it is?




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Chris Carter

unread,
Oct 16, 2013, 4:21:30 PM10/16/13
to ansible...@googlegroups.com
In the debug and error lines:

ok: [dev-build-c.oedev.us] => (item=me) => {"item": "me", "msg": "../../users/files/me.pub"}
ERROR: blah/blah/blah/provisioning/roles/users/files/.pub does not exist

The item it's supposed to be supplying is "me", so I would expect the path it derives for the lookup to be "../../users/files/me.pub", but instead it's hitting the lookup as "../../users/files/.pub", and the portion where the replacement would occur is blank (hence it's ".pub" and not "me.pub".  Does that make sense?

Chris Carter

unread,
Oct 16, 2013, 4:35:04 PM10/16/13
to ansible...@googlegroups.com
I've managed to create a test case for this (these files fall under an ansible-test directory):

## looptest.yml
---
- hosts: 127.0.0.1
  connection: local
  vars:
    mylist:
      - "item1"
  roles:
    - looptest

## roles/looptest/tasks/main.yml
---
- debug: msg="{{ item }}"
  with_items: mylist
- debug: msg="{{ lookup('file', '../../filerole/files/item1.txt') }}"
- debug: msg="{{ lookup('file', '../../filerole/files/' + item + '.txt') }}"
  with_items: mylist

## roles/filerole/files/item1.txt
Testing!

This produces the following output on Ansible 1.3.2, Python 2.7.5 (Homebrew), Mac OSX 10.8.5, using the command ansible-playbook looptest.yml -i "127.0.0.1,":

PLAY [127.0.0.1] **************************************************************

GATHERING FACTS ***************************************************************
ok: [127.0.0.1]

TASK: [debug msg=""] **********************************************************
ok: [127.0.0.1] => (item=item1) => {"item": "item1", "msg": "item1"}

TASK: [debug msg="Testing"] ***************************************************
ok: [127.0.0.1] => {"item": "", "msg": "Testing"}
ERROR: /blah/blah/blah/ansible-test/roles/filerole/files/.txt does not exist

I would expect that ERROR line to be searching for /blah/blah/blah/ansible-test/roles/filerole/files/item1.txt, not just a ".txt" file with no filename portion.

Does that help at all?

Chris

Michael DeHaan

unread,
Oct 16, 2013, 4:52:39 PM10/16/13
to ansible...@googlegroups.com
Please file a github ticket.

Thanks!

Chris Carter

unread,
Oct 16, 2013, 5:32:18 PM10/16/13
to ansible...@googlegroups.com
You bet, ticket 4549 created!
Reply all
Reply to author
Forward
0 new messages