The variable name you’re looking for is ansible_ssh_private_key_file.
You should set it at ‘vars’ level:
-
in the inventory file:
myHost ansible_ssh_private_key_file=~/.ssh/mykey1.pem myOtherHost ansible_ssh_private_key_file=~/.ssh/mykey2.pem -
in the
host_vars:# host_vars/myHost.yml ansible_ssh_private_key_file: ~/.ssh/mykey1.pem # host_vars/myOtherHost.yml ansible_ssh_private_key_file: ~/.ssh/mykey2.pem -
in a
group_varsfile if you use the same key for a group of hosts -
in the
varssection of an entry in a play:- hosts: myHost remote_user: ubuntu vars_files: - vars.yml vars: ansible_ssh_private_key_file: "{{ key1 }}" tasks: - name: Echo a hello message command: echo hello -
in setting a fact in a play entry (task):
- name: 'you name it' ansible.builtin.set_fact: ansible_ssh_private_key_file: "{{ key1 }}"
Inventory documentation