【问题标题】:Ansible: How to specify an array or list element fact with yaml?Ansible:如何使用 yaml 指定数组或列表元素事实?
【发布时间】:2016-08-08 14:25:22
【问题描述】:

当我们检查主机变量时:

  - name: Display all variables/facts known for a host
    debug: var=hostvars[inventory_hostname]

我们得到:

ok: [default] => {
    "hostvars[inventory_hostname]": {
        "admin_email": "admin@surfer190.com", 
        "admin_user": "root", 
        "ansible_all_ipv4_addresses": [
            "192.168.35.19", 
            "10.0.2.15"
        ],...

如何指定"ansible_all_ipv4_addresses" 列表的第一个元素?

【问题讨论】:

    标签: ansible yaml ansible-facts


    【解决方案1】:

    这应该就像在 Python 中一样工作。这意味着您可以使用引号访问键和使用整数的索引。

      - set_fact:
          ip_address_1: "{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'][0] }}"
          ip_address_2: "{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'][1] }}"
    
      - name: Display 1st ipaddress
        debug:
          var: ip_address_1
      - name: Display 2nd ipaddress
        debug:
          var: ip_address_2
    

    【讨论】:

      【解决方案2】:

      使用点符号

      "{{ ansible_all_ipv4_addresses.0 }}"
      

      【讨论】:

      • 如果您发现自己的情况不起作用,请检查数组结构。 VMWare 磁盘信息作为数组返回,但数组索引是一个数字字符串,所以我必须这样做才能让它工作:Ansible 2.9 中的“{{ disk_info.guest_disk_info['0'].backing_filename }}”。 5
      • 请记住,如果字典键中有破折号-,则必须将其引用并括在括号中。例如。 stats.output.0['rpc-reply'].statistics
      【解决方案3】:

      Ansible 中尝试解析命令的结果时,我遇到了同样的挑战。

      所以结果是:

      {
        "changed": true,
        "instance_ids": [
          "i-0a243240353e84829"
        ],
        "instances": [
          {
            "id": "i-0a243240353e84829",
            "state": "running",
            "hypervisor": "xen",
            "tags": {
              "Backup": "FES",
              "Department": "Research"
            },
            "tenancy": "default"
          }
          ],
          "tagged_instances": [],
        "_ansible_no_log": false
      }
      

      我想将state 的值解析到ansible playbook 中的result 寄存器中。

      我是这样做的

      由于结果是散列数组的散列,即 stateinstances 数组的索引 (0) 散列中,因此我将我的剧本修改为如下所示:

      ---
      - name: Manage AWS EC2 instance
        hosts: localhost
        connection: local
        # gather_facts: false
        tasks:
        - name: AWS EC2 Instance Restart
          ec2:
            instance_ids: '{{ instance_id }}'
            region: '{{ aws_region }}'
            state: restarted
            wait: True
          register: result
      
        - name: Show result of task
          debug:
            var: result.instances.0.state
      

      我使用register 将命令的值保存在名为result 的变量中,然后使用以下方法在变量中获取state 的值:

      result.instances.0.state
      

      这次运行命令,我得到的结果是:

      TASK [Show result of task] *****************************************************
      ok: [localhost] => {
          "result.instances.0.state": "running"
      }
      

      就是这样。

      我希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-28
        • 2018-07-17
        • 1970-01-01
        相关资源
        最近更新 更多