Ansible AWS:无法连接到EC2实例 [英] Ansible AWS: Unable to connect to EC2 instance

查看:111
本文介绍了Ansible AWS:无法连接到EC2实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个Ansible剧本创建一个安装了LAMP堆栈的EC2实例.

I want to create an EC2 instance with LAMP stack installed using one Ansible playbook.

实例创建工作正常,我可以在EC2控制台中对其进行修改,但是在尝试访问实例(例如安装apache或创建密钥)时出现问题.

The instance creation works fine, and I can modify it in the EC2 Console, but the problem appears when trying to access the instance for example install apache or create keys.

这是错误:

致命:[35.154.26.86]:无法到达! => { 已更改":错误, "msg":"[Errno无]无法连接到35.154.26.86上的端口22", 无法访问":是 }

fatal: [35.154.26.86]: UNREACHABLE! => { "changed": false, "msg": "[Errno None] Unable to connect to port 22 on or 35.154.26.86", "unreachable": true }

错误屏幕截图

这是我的剧本:

---
- name: Power up an ec2 with LAMP stack installed
  hosts: localhost
  become: true
  become_user: root
  gather_facts: False
  vars:
    keypair: myKeyPair
    security_group: launch-wizard-1
    instance_type: t2.micro
    image: ami-47205e28
    region: x-x-x
  tasks:
    - name: Adding Python-pip
      apt: name=python-pip state=latest

    - name: Install Boto Library
      pip: name=boto

    - name: Launch instance (Amazon Linux)
      ec2:
         key_name: "{{ keypair }}"
         group: "{{ security_group }}"
         instance_type: "{{ instance_type }}"
         image: "{{ image }}"
         wait: true
         region: "{{ region }}"
         aws_access_key: "xxxxxxxxxxxxxxxxxxx"
         aws_secret_key: "Xxxxxxxxxxxxxxxxxxx"
      register: ec2

    - name: Print all ec2 variables
      debug: var=ec2

    - name: Add all instance public IPs to host group
      add_host: hostname={{ item.public_ip }} groups=ec2hosts
      with_items: "{{ ec2.instances }}"


- hosts: ec2hosts
  remote_user: ec2-user
  become: true
  gather_facts: false
  tasks:
#I need help here, don't know what to do.
    - name: Create an EC2 key
      ec2_key:
        name: "privateKey"
        region: "x-x-x"
        register: ec2_key

    - name: Save private key
      copy: content="{{ ec2_key.private_key }}" dest="./privateKey.pem" mode=0600
      when: ec2_key.changed

    # The Rest is installing LAMP

信息:

1-我的主机文件是默认文件.

Information:

1- My hosts file is default.

2-我使用此命令来运行剧本:

2- I used this command to run the playbook:

sudo ansible-playbook lamp.yml -vvv -c paramiko

sudo ansible-playbook lamp.yml -vvv -c paramiko

3- launch-wizard-1具有SSH.

3- launch-wizard-1 has SSH.

4- myKeyPair是从我的设备导入到控制台的公钥(不知道这样是否可以)

4- myKeyPair is a public key imported from my device to the console(don't know if this is ok)

5-我是个大新手

推荐答案

Ansible要求在VM上安装Python才能正常工作.

Ansible requires Python installed on VM to work.

这是您所需的代码:

  - name: upload an ssh keypair to ec2
    hosts: localhost
    connection: local
    gather_facts: False
    vars:
      keypair_name: Key_name
      key_material: "{{ lookup('file', 'keyfile') }}"
      region: "{{ region }}"


    tasks:
      - name: ssh keypair for ec2
        ec2_key:
          aws_access_key: "xxxxxxxxxxxxxxxxxxx"
          aws_secret_key: "Xxxxxxxxxxxxxxxxxxx"
          region: "{{ region }}"
          name: "{{ keypair_name }}"
          key_material: "{{ key_material }}"
          state: present


  - name: Power up an ec2 with LAMP stack installed
    hosts: localhost
    become: true
    become_user: root
    gather_facts: False
    vars:
      keypair: myKeyPair
      security_group: launch-wizard-1
      instance_type: t2.micro
      image: ami-47205e28
      region: x-x-x
      my_user_data: |   # install Python: Ansible needs Python pre-installed on the instance to work!
        #!/bin/bash
        sudo apt-get install python -y

    tasks:
      - name: Adding Python-pip
        apt: name=python-pip state=latest

      - name: Install Boto Library
        pip: name=boto

      - name: Launch instance (Amazon Linux)
        ec2:
           key_name: "{{ keypair }}"
           group: "{{ security_group }}"
           instance_type: "{{ instance_type }}"
           image: "{{ image }}"
           wait: true
           wait_timeout: 300
           user_data: "{{my_user_data}}"
           region: "{{ region }}"
           aws_access_key: "xxxxxxxxxxxxxxxxxxx"
           aws_secret_key: "Xxxxxxxxxxxxxxxxxxx"
        register: ec2

      - name: Add all instance public IPs to host group
        add_host: hostname={{ item.public_ip }} groups=ec2hosts
        with_items: "{{ ec2.instances }}"

这篇关于Ansible AWS:无法连接到EC2实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆