Can I override Ansible SSH key inventory variable ansible_ssh_private_key_file on command line?

apa64 picture apa64 · May 25, 2018 · Viewed 7.6k times · Source

In Ansible 2.4.2.0, can I override the SSH key set in inventory with ansible_ssh_private_key_file on command line? It is not possible with --private-key option as reported on this issue: Command-line flags (variable weight) not respected (bug or a feature?)

The inventory file:

[group1]
instance1   ansible_host=host1  ansible_user=user1  ansible_ssh_private_key_file=~/.ssh/user1host1-id_rsa

Command line:

ansible-playbook playbook1.yml --private-key=some_other-id_rsa

Answer

techraf picture techraf · May 25, 2018

You can override any variable using --extra-vars parameter which takes the priority over variables defined in any other place:

ansible-playbook playbook1.yml --extra-vars="ansible_ssh_private_key_file=some_other-id_rsa"