【问题标题】:Getting the IP address/attributes of the AWS instance created using Ansible获取使用 Ansible 创建的 AWS 实例的 IP 地址/属性
【发布时间】:2021-01-25 22:01:36
【问题描述】:

我知道如何使用 Ansible 创建 AWS 实例。现在我想要实现的是通过使用创建实例的同一 playbook 安装 nginx 来将该实例配置为 Web 服务器。

剧本的目标是:

  1. 创建一个 AWS 实例。
  2. 通过设置 Nginx 服务器将实例配置为 Web 服务器。

ansible 可以吗?

【问题讨论】:

    标签: amazon-web-services ansible ansible-playbook


    【解决方案1】:

    阅读http://www.ansible.com/blog/ansible-ec2-tags 它详细介绍了如何启动一个(或多个)ec2 实例,然后针对它运行任务(即安装 nginx)。

    如果你想直接跳到示例剧本https://github.com/chrismeyersfsu/playbook-ec2_properties/blob/master/new_group.yml

    • 启动 ec2 实例
    • 等待 ssh
    • 将 ec2 实例添加到带有关联 ec2 pem 文件的 Ansible 动态创建的主机组(这样您就可以通过 ssh 访问它)
    • 调用一个带有 ping 任务的示例 play 以显示一切正常

    注意:您可以将 ping 任务替换为您的任务集以安装 nginx

    @Bidyut 如何引用ec2 ip地址

    查看Line 27注意register: ec2的使用,然后Line 46的ec2 ip地址被“提取”{{ ec2.results[item.0]['instances'][0]['public_ip'] }}。请注意,该示例在循环中调用register。如果您只是创建一个 ec2 实例,那么 ec2 ip 地址参考将类似于 {{ ec2.results['instances'][0]['public_ip'] }}

    【讨论】:

    • 您能解释一下如何在 Ansible playbook 中从 AWS 返回的 JSON 结果中获取/获取特定字段吗?我试图用谷歌搜索它,但我没有找到任何东西。
    • 该行中的“项目”是什么?
    • 在'public_ip'中它的值为None
    • item 的描述。我正在循环 ec2_instances 这是一个与 ec2 平行的数组 item.0 是一个索引(即 0、1、2)。
    • @Bidyut 使用- debug: msg="{{ ec2 }}" 查看ec2 变量的结构是什么样的。您应该会发现 public_ip 嵌套在 JSON 结构中。
    【解决方案2】:

    这是一个可能对您有所帮助的工作示例。

    ---
    - hosts: localhost
      connection: local
      gather_facts: no
      tasks:
        - name: Create the EC2 Instance
          ec2:
            region: us-east-1
            group: sg-xxxxx # Replace your Security Group here
            keypair: test-key # Replace Key here
            instance_type: t2.mirco
            image: ami-xxxxx # Replace AMI here
            vpc_subnet_id: subnet-xxxxx # Replace Subnet here
            assign_public_ip: yes
            wait: yes
            wait_timeout: 600
            instance_tags:
               Name: "My-EC2-Instance"
          register: ec2
    
        - name: Create SSH Group to login dynamically to EC2 Instance
          add_host: 
            hostname: "{{ item.public_ip }}"
            ansible_ssh_private_key_file: path/to/test-pair.pem
            groupname: ec2_server
          with_items: ec2.instances
    
        - name: Wait for SSH to come up
          wait_for: 
            host: "{{ item.public_ip }}" 
            port: 22 
            state: started
          with_items: ec2.instances
    
     - hosts: ec2_server
       become: yes
       # Use ec2_user if you are using CentOS/Amazon server
       remote_user: ubuntu # for Ubuntu server
       gather_facts: yes
       roles:
         - webserver
    

    【讨论】:

      【解决方案3】:

      是的,您可以使用单个 playbook 来启动实例并安装 nginx。使用 ansible 模块 add_host 添加刚刚启动的实例的 ip。然后为新主人写一出戏。

      1. 使用 ec2 模块启动 EC2 实例并注册该实例
      2. 使用 add_host 模块将新实例添加到主机清单中
      3. 用host作为刚刚注册的host写一个新的play,调用apt安装nginx

      试试看,如果需要代码 sn-p,请告诉我。

      【讨论】:

      • 我目前正在探索 Ansible。我在从 AWS 返回到 Ansible 的 JSON 输出中检索任何特定字段时遇到问题。我需要解释一下如何从 JSON 结果中获取特定字段以及如何访问结果集。
      • 我可以得到sn-p的代码吗?我想通过你的代码sn-p。
      猜你喜欢
      • 2017-01-02
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      相关资源
      最近更新 更多