首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible set_fact正在覆盖项

Ansible set_fact正在覆盖项
EN

Stack Overflow用户
提问于 2020-07-01 09:35:07
回答 2查看 113关注 0票数 2

这是我的main.yml

代码语言:javascript
复制
---
- name: Gathering VCenter facts
  vmware_vm_info:
    hostname: "{{ vcenter_server }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_pass }}"
    validate_certs: false
  register: vcenter_facts
  delegate_to: localhost

- debug:
    var: vcenter_facts.virtual_machines

- name: Find all test-vms to run IO
  set_fact:
    vm_ip: "{{ item.ip_address }}"
  loop: "{{ vcenter_facts.virtual_machines }}"
  when: item.guest_name is regex("test_vm*")

- name: print vm_ip variable value
  debug:
    var: vm_ip

- name: Mount 16TB dropbox in each test vm
  shell: mount-16tb-dropbox.sh
  args:
    chdir: /usr/local/bin/
  with_items: "{{ vm_ip }}"

下面是重述:

代码语言:javascript
复制
ok: [localhost] => {
    "vcenter_facts.virtual_machines": [
        {
            "attributes": {},
            "cluster": "Compute Cluster",
            "esxi_hostname": "100.80.90.179",
            "guest_fullname": "CentOS 7 (64-bit)",
            "guest_name": "test_vm4",
            "ip_address": "192.168.202.13",
            "mac_address": [
                "00:50:56:9d:d2:99"
            ],
            "power_state": "poweredOn",
            "tags": [],
            "uuid": "421d7b54-1359-14e8-3ec4-74b568cb96d2",
            "vm_network": {
                "00:50:56:9d:d2:99": {
                    "ipv4": [
                        "192.168.202.13"
                    ],
                    "ipv6": [
                        "fe80::44f6:a395:cde3:4dd1",
                        "fe80::a357:a163:e44f:2086",
                        "fe80::cd0c:e7d7:1356:2830"
                    ]
                }
            }
        },
        {
            "attributes": {},
            "cluster": "Compute Cluster",
            "esxi_hostname": "100.80.90.178",
            "guest_fullname": "CentOS 7 (64-bit)",
            "guest_name": "test_vm3",
            "ip_address": "192.168.202.12",
            "mac_address": [
                "00:50:56:9d:a9:e8"
            ],
            "power_state": "poweredOn",
            "tags": [],
            "uuid": "421d9239-0980-80c1-bca4-540efd726452",
            "vm_network": {
                "00:50:56:9d:a9:e8": {
                    "ipv4": [
                        "192.168.202.12"
                    ],
                    "ipv6": [
                        "fe80::cd0c:e7d7:1356:2830"
                    ]
                }
            }
        },
        {
            "attributes": {},
            "cluster": "Compute Cluster",
            "esxi_hostname": "100.80.90.178",
            "guest_fullname": "CentOS 7 (64-bit)",
            "guest_name": "Test_Automation_CentOS8_Linux_VM",
            "ip_address": "192.168.202.6",
            "mac_address": [
                "00:50:56:9d:13:14"
            ],
            "power_state": "poweredOn",
            "tags": [],
            "uuid": "421d53ba-4824-57e4-06fd-fba0f2b1dbea",
            "vm_network": {
                "00:50:56:9d:13:14": {
                    "ipv4": [
                        "192.168.202.6"
                    ],
                    "ipv6": [
                        "fe80::cd0c:e7d7:1356:2830",
                        "fe80::44f6:a395:cde3:4dd1"
                    ]
                }
            }
        },
        {
            "attributes": {},
            "cluster": "Compute Cluster",
            "esxi_hostname": "100.80.90.180",
            "guest_fullname": "CentOS 7 (64-bit)",
            "guest_name": "test_vm5",
            "ip_address": "192.168.202.14",
            "mac_address": [
                "00:50:56:9d:85:b6"
            ],
            "power_state": "poweredOn",
            "tags": [],
            "uuid": "421d6855-e60e-cd80-f113-39f11927d63b",
            "vm_network": {
                "00:50:56:9d:85:b6": {
                    "ipv4": [
                        "192.168.202.14"
                    ],
                    "ipv6": [
                        "fe80::44f6:a395:cde3:4dd1",
                        "fe80::cd0c:e7d7:1356:2830",
                        "fe80::a357:a163:e44f:2086"
                    ]
                }
            }
        }
    ]
}

我无法遍历所有的ip_address变量(即192.168.202.12192.168.202.13192.168.202.14)。

它只读取最后一项(即192.168.202.14)。

如果set_fact没有读取所有变量并执行接下来的任务集,那么我可能做错了什么呢?

EN

回答 2

Stack Overflow用户

发布于 2020-07-01 11:17:24

这应该会给出所有的IP。您正确地假定代码可能需要更正。在代码中,vm_ip变量被每个循环覆盖,最后一个IP保持不变。您需要的是一个列表,然后将每个IP附加到列表中。

代码语言:javascript
复制
- set_fact: 
    vm_ip: "{{ vm_ip | default([]) + [item.ip_address] }}"
  loop: "{{ vcenter_facts.virtual_machines | flatten }}"
  when: item.guest_name is regex("test_vm*")

- debug:
    var: vm_ip

使用Jinja2 filters的替代解决方案。

代码语言:javascript
复制
- set_fact:
    vm_ip: >- 
        {{ vcenter_facts.virtual_machines | flatten 
        | rejectattr('guest_name', 'match', '^(?!test_vm).*') 
        | map(attribute='ip_address') | list }}
票数 2
EN

Stack Overflow用户

发布于 2020-07-01 11:25:37

使用json_query的替代解决方案

代码语言:javascript
复制
---
- name: Gathering VCenter facts
  vmware_vm_info:
    hostname: "{{ vcenter_server }}"
    username: "{{ vcenter_user }}"
    password: "{{ vcenter_pass }}"
    validate_certs: false
  register: vcenter_facts
  delegate_to: localhost

- name: Mount 16TB dropbox in each test vm
  shell: mount-16tb-dropbox.sh
  args:
    chdir: /usr/local/bin/
  vars:
    query: >-
      [?contains("guest_name", 'test_vm')].ip_address[]
  with_items: "{{ vcenter_facts.virtual_machines | to_json | from_json | json_query(query) | list }}"

注意:to_json | from_json是ansible和jmespath之间的错误的一种解决方案,这样所有的值都可以转换成真正的字符串,并且可以与jmespath contains函数一起使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62673883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档