Score:0

Ansible fails to ssh connect to dynamically created EC2 host

cn flag
TSG

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
flowerysong avatar
th flag
A few notes: "not having much luck" is not a good description of the problem you're encountering with `add_host`. If at all possible you should switch to `ec2_instance` instead of using `ec2`; this module is deprecated because the library it is built on (boto) has been unmaintained since 2018. Finally, using `delegate_to: localhost` instead of `local_action` is generally preferable, since it requires maintainers to learn fewer pieces of syntax and makes it obvious that this works the same way as delegation to any other target.
Score:0
th flag

You can set variables on any task, block, or play using the vars keyword.

- name: Wait for SSH to come up
  delegate_to: "{{ ec2.instances[0].public_dns_name }}"
  wait_for_connection:
    delay: 60
    sleep: 15
    timeout: 40
  vars:
    ansible_user: centos
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.