【问题标题】:Ansible instance not appearing on AWS consoleAnsible 实例未出现在 AWS 控制台上
【发布时间】:2017-08-17 19:48:58
【问题描述】:

所以我在我的 MBP 上使用 Ansible 来尝试创建 key_pair 和创建/配置 EC2 实例。 Playbook 运行良好,没有错误,但是当我检查 AWS 控制台时,没有新密钥,也没有新实例... Ping 到据称创建的公共 IP 超时,所以我认为有些事情失败了。 Ansible 肯定会命中 AWS,因为如果我禁用 AWS 访问密钥,那么 Ansible 就会出错,并且在第二个任务中不使用 Ansible 创建的密钥也会失败,所以必须创建一个密钥,只是没有上传到 AWS?

你能发现我做错了什么吗?

Playbook yaml content:
- name: Create a sandbox instance
  hosts: localhost
  gather_facts: False
  vars:
    instance_type: t2.micro
    image: ami-d1315fb1
    region: us-west-1

  tasks:
    - name: Generate key
      ec2_key:
         name: ansible_key
         region: "{{ region }}"
         aws_access_key: #my_key
         aws_secret_key: #my_key
         state: present

    - name: Launch instance
      ec2:
         key_name: ansible_key
         group: default
         instance_type: "{{ instance_type }}"
         image: "{{ image }}"
         wait: true
         region: "{{ region }}"
         aws_access_key: #my_key
         aws_secret_key: #my_key
      register: ec2
    - name: Print all ec2 variables
      debug: var=ec2

Playbook 运行良好,输出为:

PLAY [Create a sandbox instance] ***********************************************

TASK [Generate key] ************************************************************
ok: [localhost]

TASK [Launch instance] *********************************************************
changed: [localhost]

TASK [Print all ec2 variables]                 *************************************************
ok: [localhost] => {
    "ec2": {
        "changed": true, 
        "instance_ids": [
            "i-0898f09f8d3798961"
        ], 
        "instances": [
            {
                "ami_launch_index": "0", 
                "architecture": "x86_64", 
                "block_device_mapping": {
                    "/dev/sda1": {
                        "delete_on_termination": true, 
                        "status": "attached", 
                        "volume_id": "vol-04e9c4c4f5d85e60d"
                    }
                }, 
                "dns_name": "ec2-54-215-253-115.us-west1.compute.amazonaws.com", 
                "ebs_optimized": false, 
                "groups": {
                    "sg-778b5711": "default"
                }, 
                "hypervisor": "xen", 
                "id": "i-0898f09f8d3798961", 
                "image_id": "ami-d1315fb1", 
                "instance_type": "t2.micro", 
                "kernel": null, 
                "key_name": "ansible_key", 
                "launch_time": "2017-08-16T16:57:09.000Z", 
                "placement": "us-west-1b", 
                "private_dns_name": "ip-172-31-29-166.us-west1.compute.internal", 
                "private_ip": "172.31.29.166", 
                "public_dns_name": "ec2-54-215-253-115.us-west1.compute.amazonaws.com", 
                "public_ip": "54.215.253.115", 
                "ramdisk": null, 
                "region": "us-west-1", 
                "root_device_name": "/dev/sda1", 
                "root_device_type": "ebs", 
                "state": "running", 
                "state_code": 16, 
                "tags": {}, 
                "tenancy": "default", 
                "virtualization_type": "hvm"
            }
        ], 
        "tagged_instances": []
    }
}

PLAY RECAP     *********************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

【问题讨论】:

  • 您确定,您已从控制台选择了N.California 区域吗?
  • dang,你说得对,甚至没有看到角落里的区域切换,更习惯于 DigitalOcean 界面,其中所有实例一次列出一个相关问题 - 公共 ansible_key 已启动在 AWS 上,但私有 .pem 密钥在哪里?它没有存储在本地文件夹或 MBP 上的 ~/.ssh 文件夹中

标签: amazon-web-services amazon-ec2 console ansible instance


【解决方案1】:

这里有几件事: - 当然,您已从控制台中选择了N.California(us-west-1) 区域 - 要将密钥的私有部分存储在您的用户名下的.ssh 中,请执行以下步骤:

- name: Create an EC2 key
  ec2_key:
    name: "ansible_key"
    region: "us-west-1"
    aws_access_key: #my_key
    aws_secret_key: #my_ke
  register: ec2_key

- name: save private key
  copy:
    content: "{{ ec2_key.key.private_key }}" 
    dest: "/Users/{{lookup('env', 'USER')}}/.ssh/aws-private.pem" 
    mode: 0600
  when: ec2_key.changed

注意:从头开始运行此 playbook 以创建新密钥并将其保存到您的 ~/.ssh 目录中。

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2017-11-14
    • 2017-10-08
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2017-12-01
    相关资源
    最近更新 更多