as_file option (e.g. for saving SSH keys) for lookup module #394
Replies: 1 comment 3 replies
-
Hi @parkerfath , thanks for opening this! I don't think I'd be interested in adding this option directly to the lookup plugins, for a few reasons:
It might be helpful to see some examples: All hosts use the same key- name: Same key for each host
hosts: all
vars:
ssh_key_lookup: "{{ lookup('community.hashi_vault.vault_kv2_get', 'system/abcd123') }}"
# this could also go in inventory
ansible_ssh_private_key_file: /root/key_path
pre_tasks:
- name: Write SSH key
run_once: true
become: true
delegate_to: localhost
ansible.builtin.copy:
dest: '{{ ansible_ssh_private_key_file }}'
content: '{{ ssh_key_lookup.secret.ssh_priv }}'
mode: 0600
tasks:
- name: Regular task
namespace.collection.module: Each host uses a unique key- name: Different key per host
hosts: all
vars:
ssh_key_vault_path: 'system/{{ inventory_hostname }}/abcd123'
ssh_key_lookup: "{{ lookup('community.hashi_vault.vault_kv2_get', ssh_key_vault_path) }}"
# this could also go in inventory
ansible_ssh_private_key_file: '/root/key_file_{{ inventory_hostname }}'
pre_tasks:
- name: Write SSH key
become: true
delegate_to: localhost
ansible.builtin.copy:
dest: '{{ ansible_ssh_private_key_file }}'
content: '{{ ssh_key_lookup.secret.ssh_priv }}'
mode: 0600
tasks:
- name: Regular task
namespace.collection.module: In either case you can also replace the lookup with an additional task before the one that writes the file: - name: Different key per host
hosts: all
vars:
ssh_key_vault_path: 'system/{{ inventory_hostname }}/abcd123'
# this could also go in inventory
ansible_ssh_private_key_file: '/root/key_file_{{ inventory_hostname }}'
pre_tasks:
- name: Lookup SSH key
register: ssh_key_lookup
delegate_to: localhost
community.hashi_vault.vault_kv2_get:
path: '{{ ssh_key_vault_path }}'
- name: Write SSH key
become: true
delegate_to: localhost
ansible.builtin.copy:
dest: '{{ ansible_ssh_private_key_file }}'
content: '{{ ssh_key_lookup.secret.ssh_priv }}'
mode: 0600
tasks:
- name: Regular task
namespace.collection.module: The above examples (and your original example) don't show how auth is handled, which can always add some issues. If you're willing to setup and configure a Vault agent on your ansible controller, you have some other interesting avenues. The agent can use templating to write out a secret to a file on your system and keep it refreshed, in the background, independent of ansible, and then you need only refer to the file it's keeping up to date. Another option is to use the agent as your Vault host. With the agent using auto-auth, it's able to take HTTP requests that contain no authentication, and proxy them to your Vault server with the token it keeps alive in the background. To do this, you can use |
Beta Was this translation helpful? Give feedback.
-
Has anyone considered adding an as_file parameter, similar to the one in the CyberArk Conjur plugin ?
I have a use case to pull per-host SSH keys from a HashiCorp Vault, and the only way I can tell to pass an SSH key to Ansible is via a filename. So basically, what I'd like to do is:
ansible_ssh_private_key_file: "{{ lookup('hashi_vault', 'secret/data/system/abcd123:ssh_priv', as_file=True) }}"
I would set this as a host variable, so that I could pull per-host SSH private keys from HashiCorp Vault.
Of course, it would be easier if I could use one private key across all our hosts, but our corporate policy/pen testers have required that we use separate keys per host to limit the blast radius of losing any single SSH key.
EDIT: for what it's worth, here's the PR for the change in the Conjur plugin: cyberark/ansible-conjur-collection#51
Beta Was this translation helpful? Give feedback.
All reactions