concatenate variables and strings in file lookup

4,722 views
Skip to first unread message

Colin Byrne

unread,
Jan 7, 2016, 3:16:18 PM1/7/16
to Ansible Project
Hey so I have a list of developer names in a dictionary and I am trying to do a file lookup on their ssh key, 

I want to dynamically build the file path for each key as opposed to specifying it the full path for each developer.

Here is my task:

---
 
- name: Set up authorized_keys for the root user
    authorized_key
: user=root key="{{ lookup('file', {{ role_path }} + '/files/public_keys/' + {{ item.1 }} ) }}"
    with_subelements
:
     
- ssh_users
     
- names



I haven't been able to get those variables to evaluate within the file lookup (I have tried many other syntaxes to try to achieve this).  I know I am importing them properly as I can get them to evaluate outside of the file lookup with something like the following, but then they whole string is not evaluated as a file. 


---

  - name: Set up authorized_keys for the root user

   authorized_key: user=root key={{ role_path }} + '/files/public_keys/' + {{ item.1 }}

   with_subelements:

     - ssh_users

     - names



Here is my dictionary with the embedded lists:

---
ssh_users
:
 
- group: admin
    names
:
       
- bob
       
- dave
 
- group: developers
    names
:
       
- tim
       
- susan
 
- group: analysts
    names
:
       
- mike
       
- steve



I am open to changing the task in an way shape or form to achieve this. 

Thanks for any help!    


Brian Coca

unread,
Jan 7, 2016, 4:27:29 PM1/7/16
to Ansible Project
moustaches don't stack and concatenation (+) does not work outside moustaches:

authorized_key: user=root key="{{ lookup('file', role_path +
'/files/public_keys/' + item.1) }}"
> --
> 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.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/605d42f9-0caa-46b1-929b-cd0d662e4ad4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Colin Byrne

unread,
Jan 7, 2016, 4:46:28 PM1/7/16
to Ansible Project
Ha, awesome. Thanks a lot Brian, you are the man... 
Reply all
Reply to author
Forward
0 new messages