使用Ansible启动AWS EC2实例的最佳方法 [英] Best way to launch aws ec2 instances with ansible

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

问题描述

我正在尝试在Amazon AWS上创建一个具有ansible的小型Web应用程序基础架构,我想完成所有过程:启动实例,配置服务等,但是我找不到合适的工具或模块来处理该问题。从ansible。主要是EC2启动。

I'm trying to create an small webapp infrastructure with ansible on Amazon AWS and I want to do all the process: launch instance, configure services, etc. but I can't find a proper tool or module to deal with that from ansible. Mainly EC2 Launch.

非常感谢。

推荐答案

这是您问题的简短答案,如果您想获得详细信息和完全自动化的角色,请告诉我。谢谢

This is the short answer of your question, if you want detail and fully automated role, please let me know. Thanks

先决条件


  • Ansible

  • Ansible

Python boto库

Python boto library

在环境设置

(最好在〜。/ boto内部)

Set up the AWS access and secret keys in the environment settings
(best is inside the ~./boto)

要创建EC2实例,请修改这些参数,您可以在 vars下的 ec2_launch.yml文件中找到这些参数:

In order to create the EC2 Instance, please modified these parameters that you can find inside the "ec2_launch.yml" file under "vars":


  • 区域编号要在其中启动实例的地方,美国,澳大利亚,爱尔兰等

  • count#您要创建的实例数

  • region # where is want to launch the instance(s), USA, Australia, Ireland etc
  • count # Number of instance(s), you want to create

您已经提到了这些参数,请运行以下命令:

Once, you have mentioned these parameter, please run the following command:


ansible-playbook -i主机ec2_launch.yml

ansible-playbook -i hosts ec2_launch.yml

主机文件的内容:

[local]
localhost

[webserver]

ec2_launch.yml 文件的内容:

---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t1.micro
      security_group: webserver # Change the security group name here
      image: ami-98aa1cf0 # Change the AMI, from which you want to launch the server
      region: us-east-1 # Change the Region
      keypair: ansible # Change the keypair name
      count: 1

    # Task that will be used to Launch/Create an EC2 Instance
    tasks:

      - name: Create a security group
        local_action: 
          module: ec2_group
          name: "{{ security_group }}"
          description: Security Group for webserver Servers
          region: "{{ region }}"
          rules:
            - proto: tcp
              type: ssh
              from_port: 22
              to_port: 22
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 80
              to_port: 80
              cidr_ip: 0.0.0.0/0
          rules_egress:
            - proto: all
              type: all
              cidr_ip: 0.0.0.0/0


      - name: Launch the new EC2 Instance
        local_action: ec2 
                      group={{ security_group }} 
                      instance_type={{ instance_type}} 
                      image={{ image }} 
                      wait=true 
                      region={{ region }} 
                      keypair={{ keypair }}
                      count={{count}}
        register: ec2

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
        local_action: lineinfile 
                      dest="./hosts" 
                      regexp={{ item.public_ip }} 
                      insertafter="[webserver]" line={{ item.public_ip }}
        with_items: "{{ ec2.instances }}"


      - name: Wait for SSH to come up
        local_action: wait_for 
                      host={{ item.public_ip }} 
                      port=22 
                      state=started
        with_items: "{{ ec2.instances }}"

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: "{{ ec2.instances }}"
        args:
          tags:
            Name: webserver

这篇关于使用Ansible启动AWS EC2实例的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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