Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josephbolus/dc98a5e7c9fd89d94116031e4b6fa61b to your computer and use it in GitHub Desktop.
Save josephbolus/dc98a5e7c9fd89d94116031e4b6fa61b to your computer and use it in GitHub Desktop.
Specifying ssh key in ansible playbook file

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment