Skip to content

Instantly share code, notes, and snippets.

@OlegGorj
Created January 13, 2018 18:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save OlegGorj/23c48f839bf54d2ec3884bfa19d8d6a8 to your computer and use it in GitHub Desktop.
Save OlegGorj/23c48f839bf54d2ec3884bfa19d8d6a8 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