【问题标题】:Ansible Fact - Accessing the additional factsAnsible Fact - 访问附加事实
【发布时间】:2022-10-23 03:44:06
【问题描述】:

我正在对 Ansible 事实进行一些研究。我正在使用类似的东西访问调试模块中的事实:ansible_facts['mounts']。我注意到字典中还有其他事实,例如“fstype”等。但是,当我尝试像ansible_facts['mounts']['fstype'] 这样访问它时,但我似乎这不是访问它的正确方法。我正在使用when 测试条件来检查fstype。有谁知道如何访问这个?

在大家的帮助下,这是我想出的帮助我研究的解决方案:

---
- name: Conditionals test
  hosts: dev

  tasks:
    - name: Update the kernel if suff space
      package:
        name: kernel
        state: latest
      loop: "{{ ansible_facts['mounts'] }}"
      when: item.mount == "/boot" and item.size_available > 20000000

我正在遍历 ansible_facts 列表并检查 /boot 并测量大小。谢谢大家!

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    为了更好地理解 Ansible 事实的数据结构,可以使用以下示例和测试手册。

    ---
    - hosts: localhost
      become: false
      gather_facts: true
    
      tasks:
    
      - name: Show amount of mounts
        debug:
          msg:
            - "{{ ansible_facts.mounts | type_debug }}"
            - "{{ ansible_facts.mounts | length }}"
    
      - name: Show mount type (sequence)
        debug:
          msg:  "{{ ansible_facts.mounts[item | int].fstype }}"
        loop: "{{ range(0, ansible_facts.mounts | length) | list }}"
    
      - name: Show mount type (fact list)
        debug:
          msg: "{{ item.fstype }}"
        loop: "{{ ansible_facts.mounts }}"
        loop_control:
          extended: true
          label: "{{ ansible_loop.index0 }}"
    

    表明

    • 那个ansible_facts.mounts多是一个列表,如何获取数据类型,如何获取长度
    • 如何遍历事实列表with_sequence 数字
    • 如何通过索引号访问列表元素 (ansible_facts.mounts[item | int].fstype)
    • 如何遍历事实列表并控制label

    并导致输出

    TASK [Show mount type (sequence)] ******
    ok: [localhost] => (item=0) =>
      msg: ext4
    ok: [localhost] => (item=1) =>
      msg: ext4
    ok: [localhost] => (item=2) =>
      msg: ext4
    ok: [localhost] => (item=3) =>
      msg: vfat
    
    TASK [Show mount type (fact list)] ******
    ok: [localhost] => (item=0) =>
      msg: ext4
    ok: [localhost] => (item=1) =>
      msg: ext4
    ok: [localhost] => (item=2) =>
      msg: ext4
    ok: [localhost] => (item=3) =>
      msg: vfat
    

    更多文档

    【讨论】:

    • 这是一个很好的例子。我没有使用 loop_control 语句,所以这对我来说是新事物。这确实完成了工作,我可以使用这种方法探索每个变量。
    【解决方案2】:

    我认为您的问题是您正在尝试访问坐骑列表,您需要从该列表中获取一项并获取他的 fstype 类似:

    ansible_facts['mounts'][0]['fstype']
    

    或使用循环

    - name: Print fstypes
      debug:
        var: "{{ item }}.fstype"
      loop: "{{ ansible_facts.mounts }}"
    

    【讨论】:

    • 在 Ansible v2.9 中,我遇到(语法)错误,无法让您的代码 sn-p 运行。不应该是msg: "{{ item.fstype }}" loop: "{{ ansible_facts.mounts }}"吗?
    • 是的,那是真的,我很快就发布了,但没有正确写。
    【解决方案3】:

    或者你可以这样做......

        - name: Update the kernel if suff space
          package:
            name: kernel
            state: latest
          when: ansible_facts['mounts']|selectattr('mount','equalto','/boot')|map(attribute='size_available')|first > 20000000
    
    

    而不是在所有的坐骑中循环。

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多