gpt4 book ai didi

amazon-web-services - 使用 ansible 启动 aws ec2 实例的最佳方式

转载 作者:行者123 更新时间:2023-12-03 10:10:33 25 4
gpt4 key购买 nike

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

非常感谢。

最佳答案

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

先决条件 :

  • Ansible
  • Python boto 库
  • 在环境设置中设置 AWS 访问 key 和 key
    (最好在 ~./boto 内)

  • 创建 EC2 实例:

    为了创建 EC2 实例,请修改您可以在“vars”下的“ec2_launch.yml”文件中找到的这些参数:
  • 地区 # 要在哪里启动实例,美国、澳大利亚、爱尔兰等
  • count # 要创建的实例数

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

  • 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

    关于amazon-web-services - 使用 ansible 启动 aws ec2 实例的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227140/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com