通过Ansible创建EC2实例因ec2失败:错误:无法识别的参数:

时间:2015-02-20 20:13:38

标签: amazon-ec2 ansible ansible-playbook

我是Ansible的新手,并按照this tutorial创建一个安全组和一个ec2实例。安全组已成功创建,但ec2实现了ec2实例失败:

  

错误:无法识别的参数:   /home/ec2-user/.ansible/tmp/ansible-tmp-14244 ....

我确实正确设置了aws凭证和不可变变量,如下所示

# AWS Credentials
export AWS_ACCESS_KEY_ID=xxx
export AWS_SECRET_ACCESS_KEY=xxx

# EC2 Environment Variables
export ANSIBLE_HOSTS=/etc/ansible/ec2.py
export EC2_INI_PATH=/etc/ansible/ec2.ini

文件和输出如下所示。对这个问题的任何想法?谢谢你的帮助!

$ cat group_vars/all
# Variables listed here are applicable to all host groups 
key_name: sobrr-staging.pem
aws_region: cn-north-1
ami_id: ami-9e0c9ea7
instance_type: m1.small

$ cat basic-create.yml
# Basic provisioning example
- name: Create AWS resources
  hosts: localhost
  connection: local
  gather_facts: False
  tasks:
  - name: Create security group
    ec2_group:
      name: my-security-group
      description: "A Security group"
      region: "{{aws_region}}"
      rules:
        - proto: tcp
          type: ssh
          from_port: 22
          to_port: 22
          cidr_ip: 0.0.0.0/0
      rules_egress:
        - proto: all
          type: all
          cidr_ip: 0.0.0.0/0
    register: basic_firewall

  - name: debug basic_firewall
    debug: 'msg="{{ basic_firewall }}"'

  - name: create an EC2 instance
    local_action:
      module: ec2
      key_name: "{{key_name}}"
      region: "{{aws_region}}"
      group_id: "{{basic_firewall.group_id}}"
      instance_type: "{{instance_type}}"
      image: "{{ami_id}}"
      count: 1
      wait: yes
    register: basic_ec2

  - name: debug instance start
    debug: 'msg="{{ basic_ec2 }}"'

输出

ansible-playbook -i /etc/ansible/hosts -vvvv basic-create.yml
/usr/lib64/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)

PLAY [Create AWS resources] ***************************************************

TASK: [Create security group] *************************************************
<localhost> region=cn-north-1 description=A Security group name=my-security-group
<localhost>
<localhost>
<localhost> u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.15-98406246607462/ec2_group; rm -rf /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.15-98406246607462/ >/dev/null 2>&1']
ok: [localhost] => {"changed": false, "group_id": "sg-63fae101"}

TASK: [debug basic_firewall] **************************************************
ok: [localhost] => {
    "msg": "{'invocation': {'module_name': u'ec2_group', 'module_args': ''}, 'changed': False, 'group_id': 'sg-63fae101'}"
}

TASK: [create an EC2 instance] ************************************************
<127.0.0.1> instance_type=m1.small image=ami-9e0c9ea7 group_id=sg-63fae101 region=cn-north-1 key_name=sobrr-staging.pem
<127.0.0.1>
<127.0.0.1>
<127.0.0.1>
<127.0.0.1> u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/env python /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.54-184834253412898/ec2 /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.54-184834253412898/arguments; rm -rf /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.54-184834253412898/ >/dev/null 2>&1']
failed: [localhost -> 127.0.0.1] => {"failed": true, "parsed": false}
usage: ec2 [-h] [--list] [--host HOST] [--refresh-cache]
ec2: error: unrecognized arguments: /home/ec2-user/.ansible/tmp/ansible-tmp-1424461765.54-184834253412898/arguments


FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/ec2-user/basic-create.retry

localhost                  : ok=2    changed=0    unreachable=0    failed=1

1 个答案:

答案 0 :(得分:1)

key_name ec2模块中的Ansible参数是指您在AWS账户中上传或创建的ssh公钥(如果您想重用之前的密钥)。您可能希望验证它是否与您在AWS账户中指定的名称相匹配。

我的猜测是您的AWS账户中的密钥名称是sobrr-staging,而不是sobrr-staging.pem

尝试使用sobrr-staging,看看情况如何。