首页 > 解决方案 > 如何使用 with_items/loops 从 ansible 输出中提取值

问题描述

尝试使用以下代码行

- name: reconstruct the path to test
    set_fact: 
      folder_check: "{{ item | join('\\\\') }}"
      # | regex_replace('\\\\', ',') 
    with_items:
      - "{{ path_list }}"
    ignore_errors: yes  
    register: output

当我执行这部分代码时,我得到了 SET_FACT 任务的输出,而不是 Registered output var 的输出。

ok: [shancamw2k12.iafdomain.local] => (item=[u'C:', u'temp', u'temp1']) => {"ansible_facts": {"folder_check": "C:\\\\temp\\\\temp1"}, "changed": false, "item": ["C:", "temp", "temp1"]}
ok: [shancamw2k12.iafdomain.local] => (item=[u'C:', u'temp']) => {"ansible_facts": {"folder_check": "C:\\\\temp"}, "changed": false, "item": ["C:", "temp"]}
ok: [shancamw2k12.iafdomain.local] => (item=[u'C:']) => {"ansible_facts": {"folder_check": "C:"}, "changed": false, "item": ["C:"]}.

从这行我应该只提取"folder_check": "C:\\\\temp\\\\temp1" ; "C:\\\\temp" and C:\\.

标签: ansible

解决方案


因此,我根据您问题的内容和格式假设了一些事情。

  1. 提供给 to 变量构建任务的列表将按照您想要的顺序排列,不需要排序。
  2. 您将始终使用它们
  3. 您这样做是因为输入 set_fact 任务的项目(及其内容)的数量不是静态的
  4. C 后面的斜线数量不同,具体取决于它是单独存在还是伴随路径的其他部分。

此外,由于您正在设置一个事实,因此您不需要将其保存到寄存器中,除非您需要关于变量构建的其他元数据,而不是其输出。

- name: Build Variable
  hosts:
    - all
  gather_facts: no
  vars:
    - path_list:
        - "C:"
        - "temp"
        - "temp1"
  tasks:
    - set_fact:
        temp_var: "{{ temp_var|default([]) + [ item + ('//' if my_idx == 0 else '' if my_idx == path_list|length - 1 else '////') ] }}"
      loop: "{{ path_list }}"
      loop_control:
        index_var: my_idx

    - set_fact:
        folder_check: "{{ folder_check|default([]) + [ item if my_idx|int-1 < 0 else folder_check[my_idx|int-1] + '//' + item if my_idx|int-1 == 0 else folder_check[my_idx|int-1] + item  ] }}"
      loop: "{{ temp_var }}"
      loop_control:
        index_var: my_idx

    - debug: var=folder_check

这会给你这样的输出:

PLAY [Build Variable] ********************************************************************************************************

TASK [set_fact] **************************************************************************************************************
ok: [host1] => (item=C:) => {"ansible_facts": {"temp_var": ["C://"]}, "changed": false, "item": "C:", "my_idx": 0}
ok: [host1] => (item=temp) => {"ansible_facts": {"temp_var": ["C://", "temp////"]}, "changed": false, "item": "temp", "my_idx": 1}
ok: [host1] => (item=temp1) => {"ansible_facts": {"temp_var": ["C://", "temp////", "temp1"]}, "changed": false, "item": "temp1", "my_idx": 2}

TASK [set_fact] **************************************************************************************************************
ok: [host1] => (item=C://) => {"ansible_facts": {"folder_check": ["C://"]}, "changed": false, "item": "C://", "my_idx": 0}
ok: [host1] => (item=temp////) => {"ansible_facts": {"folder_check": ["C://", "C:////temp////"]}, "changed": false, "item": "temp////", "my_idx": 1}
ok: [host1] => (item=temp1) => {"ansible_facts": {"folder_check": ["C://", "C:////temp////", "C:////temp////temp1"]}, "changed": false, "item": "temp1", "my_idx": 2}

TASK [debug] *****************************************************************************************************************
ok: [host1] => {
    "folder_check": [
        "C://",
        "C:////temp////",
        "C:////temp////temp1"
    ]
}

推荐阅读