Thanks for that. I eventually managed to get it working via this method:
---
- hosts: all
tasks:
- set_fact:
username: "{{ username | default(ansible_user_id) }}"
- set_fact:
keyfile: "{{ keyfile | default('/home/' + username + '/.ssh/id_rsa.pub') }}"
- set_fact:
ssh_public_key: "{{ lookup('file', '{{ keyfile }}') }}"
Interestingly, or not, it fails if I attempt to collapse the three set_fact statements into one stanza:
TASK [set_fact] ***************************************************************************************************************************************************************************************************
fatal: [dgsdtstlum01.local]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'username' is undefined\n\nThe error appears to have been in '/home/mbrooks/ansible-config/books/distribute_ssh_key.yaml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - set_fact:\n ^ here\n"}
Some kind of scope guarding? Moustaches not stacking noted, thankyou.
Mart.