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

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

问题描述

我正在尝试在 Amazon AWS 上创建一个带有 ansible 的小型 webapp 基础设施,我想完成所有过程:启动实例、配置服务等,但我找不到合适的工具或模块来处理这些来自 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

先决条件:

  • 智能

Python boto 库

Python boto library

在环境设置中设置 AWS 访问和秘密密钥
(最好在 ~./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

hosts 文件的内容:

[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天全站免登陆