Is there a way to specify a password for the SSH session in config file or CLI

Is it possible to specify a password for the SSH connection in Ansible (the initial connection not the one for sudo) in a non-interactive way (ie. in config file or a command)?

The configuration file in Ansible has the option ask_pass to prompt user to input the password (works ok), but I would like to hardcode the default Raspberry Pi’s password (which is “raspberry” on the original image) for the purpose of unattended configuration of new Pis.

You can just use the ansible_ssh_pass parameter:

  • name: some task
    shell: “echo ‘hello world’”
    ansible_ssh_pass: raspberry

http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters

You mean a parameter in the playbook? Never thought it was possible, but I tried your suggestion and it did not work. I got the following (I am using 1.9.4 now):

`
ERROR: ansible_ssh_pass is not a legal parameter in an Ansible task or handler

`

The link you mentioned is however a description of the parameters for the inventory file. And it works, but I forgot to mention that I do not want to use the inventory file and provide the IP address of the destination machine as a command-line parameter (that’s why I asked this question - if the “syntax with comma” would allow passing parameters, I could do it).

Summary of my goal (initial configuration only, before I upload the ssh keys with Ansible):

  1. pass the destination IP address as a command-line parameter for ansible-playbook command
  2. hardcode user name “pi” (now in ansible.cfg)
  3. hardcode password 'raspberry" (in ansible.cfg, as a command-line parameter or elsewhere, but not in the inventory file which I do not intend to use)

My mistake. I thought i’d used it previously in a playbook, but when i went back and looked it was commented out.

So If i’m understanding all your requirements properly. I think this should work(and i’ve tested it this time).

ansible-playbook -i 192.168.0.1, playbook.yml --extra-vars "ansible_ssh_pass=rapsberry"

Thank you. That’s the solution.

But… I would like to ask if your example really did work for you?

I managed to do what I wanted, but in addition I also had to pass a variable to hosts in playbook:

ansible-playbook -i 192.168.0.1, playbook.yml --extra-vars “newpi=192.168.0.1 ansible_ssh_pass=raspberry”

and use:

- hosts: '{{ newpi }}'

in the playbook.yml

Is there a way that makes passing the newpi unnecessary?

Hmm, well in my test playbook, i just had

  • hosts: all

I’m guessing that’s why I didn’t need to pass the additional variable.

Great! I was dragging my trial-and-error results. Now its (your version) in the simplest possible form, I guess.

Thank you again.