【问题标题】:ansible module amazon.aws.ec2_instance - can't get public IPansible 模块 amazon.aws.ec2_instance - 无法获取公共 IP
【发布时间】:2021-12-30 14:06:14
【问题描述】:

我使用“旧”EC2 Ansible 模块创建了以下剧本:

---
- name: Create Ec2 instances
  hosts: localhost
  become: False
  # import the secret file
  vars_files:
    - secrets.yml
  gather_facts: false
  tasks:

  # Block is a Group of Tasks combined together
  - name: Get Info Block
    block:
      - name: Get Running instance Info

        ec2_instance_info:
        register: ec2info

      - name: Print info
        debug: var="ec2info.instances"

  - name: Create EC2 Block
    block:

      - name: Launch ec2 instances
        tags: create_ec2
        ec2:
          region: eu-west-1
          key_name: ec2-tutorial
          group: launch-wizard-1
          instance_type: t2.micro
          image: ami-04dd4500af104442f
          wait: yes
          wait_timeout: 500
          count: 1
          instance_tags:
            name: appservers
            os: ubuntu
          monitoring: no
          vpc_subnet_id: subnet-049055a2c1633c0eb
          assign_public_ip: yes
          aws_access_key: XXXX
          aws_secret_key: YYYYYYY
        register: ec2
        delegate_to: localhost

      - name : Add instance to host group
        add_host:
          hostname: "{{ item.public_ip }}"
          groupname: launched
        loop: "{{ ec2.instances }}"

      - name: Wait for SSH to come up
        local_action:
          module: wait_for
          host: "{{ item.public_ip }}"
          port: 22
          delay: 10
          timeout: 120
        loop: "{{ ec2.instances }}"
    # By specifying never on the tag of this block,
    # I let this block to run only when explicitely being called
    tags: ['never', 'ec2-create']

它工作正常,我能够创建和测试 EC2 实例并检索其公共 IP 的连接。

问题是 EC2 已被弃用,所以;我使用不返回公共 IP 的新模块 ec2_instance 重写了相同的剧本。
由于我无法测试连接,所以我暂时放了一个- meta: end_play,以便我可以部署实例。
这是新的剧本

---
- name: Create Ec2 instances
  hosts: localhost
  become: False
  # import the secret file
  vars_files:
    - secrets.yml
  gather_facts: false
  tasks:

  # Block is a Group of Tasks combined together
  - name: Get Info Block
    block:
      - name: Get Running instance Info

        ec2_instance_info:
        register: ec2info

      - name: Print info
        debug: var="ec2info.instances"

    # By specifying always on the tag,
    #     # I let this block to run all the time by module_default
    #         # this is for security to net create ec2 instances accidentally
    tags: ['always', 'getinfoonly']

  - name: Create EC2 Block
    block:

      - name: Launch ec2 instances
        tags: create_ec2
        ec2_instance:
          name: "Test new Ansible ec2 module"
          region: eu-west-1
          #Availability zone is mutually excluded with vpc_subnet_id
          #availability_zone: eu-west-1c
          #key_name: ec2-tutorial
          key_name: testec2key
          security_group: launch-wizard-1
          instance_type: t2.micro
          image_id: ami-04dd4500af104442f
          wait: yes
          volumes:
            - device_name: /dev/sdb
              ebs:
                volume_size: 8
                delete_on_termination: true
            #- device_name: /dev/sdb
            #  volume_type: gp2
            #  volume_size: 5
            #  delete_on_termination: true
          wait_timeout: 500
          # count not available is this new module
          #count: 1
          tags:
            name: "Test"
            os: AwsAmi
          #vpc_subnet_id: subnet-049055a2c1633c0eb
          vpc_subnet_id: subnet-08a394b8718bbff45
          network:
            assign_public_ip: true
          #termination_protection: yes
          #aws_access_key: XXXXXXX
          #aws_secret_key: YYYYYYYYYYY
        register: ec2
        delegate_to: localhost

      #- meta: end_play

      - name: Add instance to host group
        add_host:
          hostname: "{{ item.public_ip }}"
          groupname: launched
        loop: "{{ ec2.instances }}"
               
      - name: Wait for SSH to come up
        local_action:
          module: wait_for
          host: "{{ item.public }}"
          port: 22
          delay: 10
          timeout: 120
        loop: "{{ ec2.instances }}"
    # By specifying never on the tag of this block,
    # I let this block to run only when explicitely being called
    tags: ['never', 'ec2-create']

这是我运行剧本时出现的错误,评论- meta: end_play(这使得剧本继续下去):

ansible-playbook aws-ec2-creationtst3.yml --connection=local --tags=ec2-create -e "ansible_python_interpreter=/home/marcoreale/aws-venv/bin/python3" --ask-vault-pass -vvvv

TASK [Add instance to host group] **************************************************************************************
task path: /home/xxx/aws-venv/playbook/aws-ec2-create/aws-ec2-creationtst3.yml:70
fatal: [localhost]: FAILED! => {
   "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'public_ip'\n\nThe error appears to be in '/home/marcoreale/aws-venv/playbook/aws-ec2-create/aws-ec2-creationtst3.yml': line 70, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n      - name: Add instance to host group\n        ^ here\n"
}

注意:用 item.private_ip 替换 item.public_ip 可以,但我需要使用公共 IP 测试连接。
你有什么建议吗?我应该如何更改我的剧本以使用公共 IP 测试连接?

【问题讨论】:

    标签: amazon-ec2 module ansible


    【解决方案1】:

    文档指出公共 IP 地址现在通过 public_ip_address 属性嵌套。

    所以,你的任务应该适用

    - name: Add instance to host group
      add_host:
        hostname: "{{ item.public_ip_address }}"
        groupname: launched
      loop: "{{ ec2.instances }}"
    

    【讨论】:

    • 抱歉,我没有提到我已经尝试过使用“{{ item.public_ip_address }}”,但总是收到与未定义变量相关的错误。
    【解决方案2】:

    您想在您的 ec2_instance 调用中设置 state: running。基本上发生的事情是您的等待只是直到它达到定义的状态,并且默认状态不会等待分配 public_ip。

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 1970-01-01
      • 2014-04-05
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      相关资源
      最近更新 更多