Hello

I have a simple playbook that distributes my SSH key.   I tried to make a 
more generic version of the playbook that would accept arguments via -e so 
that the same playbook could distribute either a different key or a key for 
a different user.  This is what I have so far:


---
- hosts: all
  vars:
    username: "{{ username if username is defined else {{ansible_user_id}} 
}}"
    ssh_public_key: "{{ lookup('file', '{{ keyfile }}') if keyfile is 
defined else lookup('file', '/home/{{ username }}/.ssh/id_rsa.pub') }}"


  tasks:

    - debug: var=ssh_public_key
    - debug: var=username

    - name: Make sure the user's ~/.ssh exists.
      file:
        path: /home/{{ username }}/.ssh
        state: directory
        owner: "{{ username }}"
        group: domain^users
        mode: 0750

    - name: Distribute my key
      become: true
      authorized_key:
        user: "{{ username }}"
        key:  "{{ ssh_public_key }}"
        manage_dir: yes


This fails when invoked with no arguments (i.e. nothing passed via -e) but 
I simply do not understand the error message:

TASK [debug] 
******************************************************************************************************************************************************************************************************
fatal: [dgsdtstlum01.mcs.local]: FAILED! => {"failed": true, "msg": "{{ 
lookup('file', '{{ keyfile }}') if keyfile is defined else lookup('file', 
'/home/{{ username }}/.ssh/id_rsa.pub') }}: {{ username if username is 
defined else {{ansible_user_id}} }}: template error while templating 
string: expected token ':', got '}'. String: {{ username if username is 
defined else {{ansible_user_id}} }}"}

I apologise if this is a simple error, but I've been bashing at it for 
hours with no success and google isn't being forthcoming. Where exactly is 
it expecting a ":" and why?

This is ansible version 2.3.0.  Thanks for reading.

Regards

Martin.

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/57e0996d-2007-4285-ad9d-5a1274e0a748%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to