【问题标题】:Fetch the values of the variables in ansible获取ansible中变量的值
【发布时间】:2020-08-19 05:35:07
【问题描述】:

我正在尝试列出已找到匹配项的名称。

- name: search for files containing string
 find:
   paths: /root/ansible-dir
   patterns: "file3.yml"
   contains: "{{ item }}"
 with_items: "{{ names_list }}"
 register: file_match

- name: print file
  debug:
    msg: "{{ file_match }}"

上面的代码运行后,会生成下面的代码:

    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "examined": 14,
                "failed": false,
                "files": [],
                "invocation": {
                    "module_args": {
                        "age": null,
                        "age_stamp": "mtime",
                        "contains": "node_mem",
                        "depth": null,
                        "excludes": null,
                        "file_type": "file",
                        "follow": false,
                        "get_checksum": false,
                        "hidden": false,
                        "paths": [
                            "/root/ansible-dir"
                        ],
                        "patterns": [
                            "file3.yml"
                        ],
                        "recurse": false,
                        "size": null,
                        "use_regex": false
                    }
                },
                "item": "node_mem",
                "matched": 0,
                "msg": ""
            },

如何获取匹配属性>=1的项目名称?

【问题讨论】:

  • 看看这个:when

标签: ansible yaml


【解决方案1】:

使用subelements。下面的调试提示您如何迭代结果并获取包含项目的文件

    - debug:
        msg: "Fetch {{ item.1 }}"
      with_subelements:
        - "{{ file_match.results }}"
        - files

不必测试文件数>=1。如果文件列表为空,则无论如何都会跳过迭代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2019-12-27
    相关资源
    最近更新 更多