The variable name is ansible_ssh_private_key_file .
One 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:
#### hosts_vars/myHost.yml
ansible_ssh_private_key_file: ~/.ssh/mykey1.pem
#### hosts_vars/myOtherHost.yml
ansible_ssh_private_key_file: ~/.ssh/mykey2.pem
-
in a group_vars file if you use the same key for a group of hosts
-
in the vars section of your play EDIT: this solution can't work as it's too late to define the variable...
- 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
- and, the one i end up using, in host file as variable: (assuming all nodes use one key)
[all:vars]
ansible_ssh_connection=ssh ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
[my-servers]
ip-myserver-1.eu-west-1.compute.internal
ip-myserver-2.eu-west-1.compute.internal
link to inventory docs: http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters