使用Ansible设置EC2实例时遇到问题

使用Ansible设置EC2实例时遇到问题,ansible,ansible-playbook,Ansible,Ansible Playbook,我对如何使用Ansible启动EC2实例感到非常困惑 我正在尝试使用ec2.py清单脚本。我不确定应该使用哪一个,因为Ansible安装了三个: ansible/lib/ansible/module_utils/ec2.py ansible/lib/ansible/modules/core/cloud/amazon/ec2.py ansible/plugins/inventory/ec2.py 我认为运行inventory/中的一个最有意义,因此我使用以下方法运行它: ansible-pla

我对如何使用Ansible启动EC2实例感到非常困惑

我正在尝试使用ec2.py清单脚本。我不确定应该使用哪一个,因为Ansible安装了三个:

  • ansible/lib/ansible/module_utils/ec2.py
  • ansible/lib/ansible/modules/core/cloud/amazon/ec2.py
  • ansible/plugins/inventory/ec2.py
我认为运行inventory/中的一个最有意义,因此我使用以下方法运行它:

ansible-playbook launch-ec2.yaml -i ec2.py
这给了我:

msg: Either region or ec2_url must be specified
因此,我添加了一个区域(即使我指定了vpc\u子网\u id),我得到:

msg: Region us-east-1e does not seem to be available for aws module boto.ec2. If the region definitely exists, you may need to upgrade boto or extend with endpoints_path
我想亚马逊最近一定改变了ec2,所以你需要使用专有网络?即使在我尝试从Amazon的控制台启动实例时,“EC2 Classic”选项也被禁用

当我尝试在cloud/amazon/I中使用ec2.py脚本时,我得到:

ERROR: Inventory script (/software/ansible/lib/ansible/modules/core/cloud/amazon/ec2.py) had an execution error:
没有比这更详细的了

经过一些搜索,我在/module_utils中看到了ec2.py模块,因此不需要指定区域。我试图运行此文件,但得到:

错误:文件/software/ansible/lib/ansible/module_utils/ec2.py被标记为可执行文件,但未能正确执行。如果这不是一个可执行脚本,请使用
chmod-x/software/ansible/lib/ansible/module\u utils/ec2.py
更正

因此,正如错误所示,我删除了ec2.py文件的可执行权限,但随后出现以下错误:

ERROR: /software/ansible/lib/ansible/module_utils/ec2.py:30: Invalid ini entry: distutils.version - need more than 1 value to unpack

有没有人对如何让它工作有什么想法?要使用的正确文件是什么?在这一点上,我完全不知道如何让它工作。

在你的帖子中有几个问题。我将试着用三个项目来总结它们:

  • 是否仍然可以在EC2 Classic(无VPC)中启动实例
  • 如何使用Ansible创建新的EC2实例
  • 如何启动动态清单文件
    ec2.py
  • 1.EC2经典 根据您创建AWS帐户的时间、实例类型和使用的AMI虚拟化类型,您的选项将有所不同。参考文献:

    如果上述参数都不限制EC2 classic的使用,那么您应该能够创建一个新实例,而无需定义任何VPC

    2.使用Ansible创建新的EC2实例 因为您的实例还不存在,所以动态清单文件(
    ec2.py
    )是无用的。尝试指示ansible在本地计算机上运行

    创建具有以下内容的新资源清册文件,例如
    new\u hosts

    [localhost]
    127.0.0.1
    
    然后,您的剧本,例如
    create\u instance.yml
    应该使用本地连接和
    hosts:localhost
    。请参见下面的示例:

    --- # Create ec2 instance playbook
    
    - hosts: localhost
      connection: local
      gather_facts: false
      vars_prompt:
        inst_name: "What's the name of the instance?"
      vars:
          keypair: "your_keypair"
          instance_type: "m1.small"
          image: "ami-xxxyyyy"
          group: "your_group"
          region: "us-west-2"
      tasks:
        - name: make one instance
          ec2: image={{ image }}
               instance_type={{ instance_type }}
               keypair={{ keypair }}
               instance_tags='{"Name":"{{ inst_name }}"}'
               region={{ region }}
               group={{ group }}
               wait=true
          register: ec2_info
    
        - name: Add instances to host group
          add_host: hostname={{ item.public_ip }} groupname=ec2hosts
          with_items: ec2_info.instances
    
        - name: Wait for SSH to come up
          wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
          with_items: ec2_info.instances
    
    此重头戏将创建一个EC2实例,并将其公共IP注册为ansible主机变量
    ec2hosts
    ie。就像您在清单文件中定义了它一样。如果您想配置刚刚创建的实例,只需添加一个新的播放
    hosts:ec2hosts
    ,这将非常有用

    最终,按如下方式启动ansible:

    export ANSIBLE_HOST_KEY_CHECKING=false
    export AWS_ACCESS_KEY=<your aws access key here>
    export AWS_SECRET_KEY=<your aws secret key here>
    
    ansible-playbook -i new_hosts create_instance.yml
    
    对于Ansible 2:


    配置
    ec2.ini
    并运行
    ec2.py
    ,它将向stdout打印一个ini格式的主机列表。

    感谢您提供了非常详尽的答案。我很快就会试试这个。我想我对何时使用ec2动态库存感到困惑。如果您不使用它来启动实例,您会使用它做什么?您概述的剧本似乎创建了一个名为ec2hosts的新实例文件,那么您是否可以使用该清单文件与您启动的ec2服务器进行进一步交互?我将使用动态清单进行配置,例如,将nginx配置更新到所有应用服务器,假设您已经配置了一些自动缩放规则,但实际上不知道在任何给定时刻有多少应用程序服务器正在运行。在这种情况下,我将使用一个动态资源清册,它将查询EC2并返回应用程序服务器的实际列表。@BrianDiCasa关于“ec2hosts”变量。这是您所说的,实际上好像是库存文件中的一个新条目,如
    [ec2hosts]
    ,但只是保存在内存中。见模块。这是执行实例的第一次引导的好方法,例如更改主机名,/etc/sudoers,。。。但是,当剧本结束时,当前在
    [ec2hosts]
    下内存中的关于这些实例的信息将丢失。此实例的任何进一步修改都需要使用动态库存文件。
    cd /etc/ansible
    wget https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.py
    wget https://raw.githubusercontent.com/ansible/ansible/stabe-1.9/plugins/inventory/ec2.ini
    chmod u+x ec2.py
    
    cd /etc/ansible
    wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
    wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
    chmod u+x ec2.py