Ansible 中变量为空或未定义

Variable is empty or no defined in Ansible

当我 运行 我的剧本时,我看到以下错误:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe e
rror appears to be in '/etc/ansible/loyalty/tasks/create_ec2_stage.yaml': line 63, column 7, but may\nbe elsewhere in the file depending on the exac
t syntax problem.\n\nThe offending line appears to be:\n\n      register: ec2_metadata\n    - name: Parse < JSON >\n      ^ here\n"} 

我运行剧本是这样的:

/usr/bin/ansible-playbook -i hosts --extra-vars "CARRIER=xx" tasks/create_ec2_stage.yaml

这是我的剧本:

---
- name: New EC2 instances
  hosts: localhost
  gather_facts: no
  vars_files:
    - /etc/ansible/loyalty/vars/vars.yaml
  tasks:
    - name: Run EC2 Instances
      amazon.aws.ec2_instance:
        name: "new-{{ CARRIER }}.test"
        aws_secret_key: "{{ ec2_secret_key }}"
        aws_access_key: "{{ ec2_access_key }}"
        region: us-east-1
        key_name: Kiu
        instance_type: t2.medium
        image_id: xxxxxxxxxxxxx
        wait: yes
        wait_timeout: 500
        volumes:
          - device_name: /dev/xvda
            ebs:
              volume_type: gp3
              volume_size: 20
              delete_on_termination: yes
        vpc_subnet_id: xxxxxxxxxxxx
        network:
          assign_public_ip: no
        security_groups: ["xxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxxxxx"]
        tags:
          Enviroment: TEST
        count: 1
    - name: Pause Few Seconds
      pause:
        seconds: 20
        prompt: "Please wait"
    - name: Get Information for EC2 Instances
      ec2_instance_info:
        region: us-east-1
        filters:
          "tag:Name": new-{{ CARRIER }}.test
      register: ec2_metadata
    - name: Parse JSON
      set_fact:
        ip_addr: "{{ ec2_metadata.instances[0].network_interfaces[0].private_ip_address }}"

如果我创建一个稍微小一点的 playbook 来查询现有实例的私有 IP 地址,我没有看到任何错误。

---
- name: New EC2 Instances
  hosts: localhost
  gather_facts: no
  vars_files:
    - /etc/ansible/loyalty/vars/vars.yaml
  vars:
    pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
    CARRIER_UPPERCASE: "{{ CARRIER | upper }}"
  tasks:
    - set_fact:
       MY_PASS: "{{ pwd_alias }}"
    - name: Get EC2 info
      ec2_instance_info: 
        region: us-east-1
        filters:
          "tag:Name": new-{{ CARRIER }}.stage
      register: ec2_metadata
    - name: Parsing JSON
      set_fact: 
        ip_addr: "{{ ec2_metadata.instances[0].network_interfaces[0].private_ip_address }}"
    - name: Show Result
      debug:
        msg: "{{ ip_addr }}"

结果

TASK [Show Result] ******************************************************
ok: [localhost] => {
    "msg": "172.31.x.x"
}

我正在亚马逊上创建一个EC2实例并查询私有IP以在其他服务(如router53和Cloudflare)上使用该IP,其他任务不会添加它们,因为错误是事实。

您不必使用 ec2_instance_info 模块查询 AWS,因为 the module amazon.aws.ec2_instance already returns 当参数 wait 设置为 yes 时,您是新创建的 EC2,因为在你的情况下。

你只需要register这个任务的return。

所以,给定两个任务:

- name: Run EC2 Instances
  amazon.aws.ec2_instance:
    name: "new-{{ CARRIER }}.test"
    aws_secret_key: "{{ ec2_secret_key }}"
    aws_access_key: "{{ ec2_access_key }}"
    region: us-east-1
    key_name: Kiu
    instance_type: t2.medium
    image_id: xxxxxxxxxxxxx
    wait: yes
    wait_timeout: 500
    volumes:
      - device_name: /dev/xvda
        ebs:
          volume_type: gp3
          volume_size: 20
          delete_on_termination: yes
    vpc_subnet_id: xxxxxxxxxxxx
    network:
      assign_public_ip: no
    security_groups: ["xxxxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxxxxx"]
    tags:
      Enviroment: TEST
    count: 1
  register: ec2

- set_fact:
    ip_addr: "{{ ec2.instance[0].private_ip_address }}"

您应该有新创建的 EC2 实例的私有 IP 地址。