英文:
Use newest file in a directory for include_vars`
问题
我需要为位于`../roles/somerole/files/`下最后添加的文件设置默认值。但是我不能使用'find'模块,因为它尝试在远程主机上查找路径,而不是在我的本地机器上。这是一个例子。
这是我的代码块:
-
hosts: all
become: yespre_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 }}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论