Ansible实例未出现在AWS控制台上

时间:2017-08-17 08:20:14

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

所以我在我的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

1 个答案:

答案 0 :(得分:0)

以下是一些事情: - 确定,您已从控制台中选择了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目录中。