使用目录中的最新文件来进行include_vars。

huangapple go评论100阅读模式
英文:

Use newest file in a directory for include_vars`

问题

我需要为位于`../roles/somerole/files/`下最后添加的文件设置默认值。但是我不能使用'find'模块,因为它尝试在远程主机上查找路径,而不是在我的本地机器上。这是一个例子。

这是我的代码块:
  • hosts: all
    become: yes

    pre_tasks:

    • name: 获取最后添加的文件
      find:
      paths: "../roles/somerole/files/"
      register: found_files

    • name: 包含上述文件夹中最新文件的变量
      include_vars: "{{ found_files.files | sort(attribute='mtime',reverse=true) | first }}"


这是一个输出:

ok: [1.1.1.1] => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"examined": 0,
"files": [],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"depth": null,
"excludes": null,
"file_type": "file",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"../roles/somerole/files/"
],
"patterns": [
"*"
],
"read_whole_file": false,
"recurse": false,
"size": null,
"use_regex": false
}
},
"matched": 0,
"msg": "Not all paths examined, check warnings for details",


请问如何在本地获取文件,而不是在远程主机上?
英文:

I need to set up a default value for the variable the last added file under ../roles/somerole/files/. However I cant use 'find' module because it try to find path on remote host, not on my local machine. Here's an example.

This is my code block:

- hosts: all
  become: yes

  pre_tasks:
    - name: Get last added file
      find: 
        paths: "../roles/somerole/files/"
      register: found_files

    - name: Include vars from latest file in above folder
      include_vars: "{{ found_files.files | sort(attribute='mtime',reverse=true) | first }}"

And this is an output:

[WARNING]: Skipped '../roles/somerole/files/' path due to this access issue: '../roles/somerole/files/' is not a directory
ok: [1.1.1.1] => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "examined": 0,
    "files": [],
    "invocation": {
        "module_args": {
            "age": null,
            "age_stamp": "mtime",
            "contains": null,
            "depth": null,
            "excludes": null,
            "file_type": "file",
            "follow": false,
            "get_checksum": false,
            "hidden": false,
            "paths": [
                "../roles/somerole/files/"
            ],
            "patterns": [
                "*"
            ],
            "read_whole_file": false,
            "recurse": false,
            "size": null,
            "use_regex": false
        }
    },
    "matched": 0,
    "msg": "Not all paths examined, check warnings for details",

Could you please advice how can I get files locally, not on the remote host?

答案1

得分: 2

由于您要查找的文件位于控制器上,您需要将find任务委托给localhost,并使用run_once: true选项运行它,以便它不会在播放循环中的每个主机上重复执行。此外,为了使上述操作生效,您需要从结果列表中提取path属性。

- hosts: all
  become: yes

  pre_tasks:
    - name: Get last added file from localhost
      find: 
        paths: "../roles/somerole/files/"
      register: found_files
      delegate_to: localhost
      run_once: true

    - name: Include vars from latest file in above folder
      include_vars: "{{ found_files.files | sort(attribute='mtime', reverse=true) | map(attribute='path') | first }}"
英文:

Since the files you are looking for are on the controller, you will need to delegate the find task to localhost and run it with run_once: true so that it is not repeated for every host in your play loop. Moreover, for the above to work, you will need to extract the path attribute from your result list.

- hosts: all
  become: yes

  pre_tasks:
    - name: Get last added file from localhost
      find: 
        paths: "../roles/somerole/files/"
      register: found_files
      delegate_to: localhost
      run_once: true

    - name: Include vars from latest file in above folder
      include_vars: "{{ found_files.files | sort(attribute='mtime', reverse=true) | map(attribute='path') | first }}"

huangapple
  • 本文由 发表于 2023年2月27日 18:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定