My Ansible script creates an AWS machine, then tries to connect to it. Depending on the image I'm creating, the default username for SSH login is either 'centos' or 'ubuntu' etc.
My script below fails on the wait_for_connection with a 'permission denied' error. I assume this is because ssh is using the wrong username; the control node is running the script as 'userx'. (I confirmed my public key is on the remote machine, and my matching private key is available on the control node).
How do I adjust my script to cause ansible to use the correct username? I can't specify it in the inventory since the host was just created, and the username depends on the type of instance. There is not 'username' option for wait_for_connection
My script:
- name: Create one AWS machine
local_action:
module: ec2
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
key_name: "{{ key_name }}"
group_id: "{{ security_group_id }}"
instance_type: "{{ instance_type }}"
image: "{{ ami_id }}"
wait: true
region: "{{ aws_region }}"
zone: "{{ aws_zone}}"
vpc_subnet_id: "{{ aws_vpc_subnet }}"
assign_public_ip: yes
register: ec2
- name: Wait for SSH to come up
delegate_to: "{{ ec2.instances[0].public_dns_name }}"
wait_for_connection:
delay: 60
sleep: 15
timeout: 40
I tried setting the user and keyfile right before the wait_for_connection as shown below, but ansible still reports as connecting as user NONE, and the keyfiles tried do no list my keyfile (/root/.ssh/mykey):
- name: Prepare to connect to new node
set_fact:
ansible_user: 'centos'
ansible_ssh_private_key_file: "{{ private_key_file }}"
and part of the ansible output:
<ec2-34-27-194-74.compute-1.amazonaws.com> ESTABLISH SSH CONNECTION FOR USER: None
debug1: identity file /root/.ssh/id_ecdsa type -1