Specifying ssh key in ansible playbook file


Ansible playbook can specify the key used for ssh connection using --key-file on the command line.

Is it possible to specify the location of this key in playbook file instead of using --key-file on command line?

Because I want to write the location of this key into a var.yaml file, which will be read by ansible playbook with vars_files:.

The followings are parts of my configuration:

vars.yml file

playbook.yml file

I’ve tried adding ansible_ssh_private_key_file under vars. But it doesn’t work on my machine.

If I run ansible-playbook with the playbook.yml above. I got the following error:

I don’t find the name of my key file in the ssh command. It’s strange.


The variable name you’re looking for is ansible_ssh_private_key_file.

You should set it at ‘vars’ level:

  • in the inventory file:
  • in the host_vars:
  • in a group_vars file if you use the same key for a group of hosts
  • in the vars section of an entry in a play:
  • in setting a fact in a play entry (task):

Inventory documentation

Leave a Reply