在 ansible 中有 stdout.line 的替代方案吗?
Is there any alternative to stdout.line in ansible?
我在 ansible 中遇到 stdout.lines 的问题。有没有其他选择?
这是我的代码
- hosts: myhost
tasks:
- name: get contents
shell: "ls /home/*.iso | cut -d '/' -f 7 "
register: prod
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ prod.stdout_lines }}"
validate_certs: false
当我 运行 这个时,我在 UI 中得到的输出是
['test.iso']
但预期的输出是
test.iso
如有任何帮助,我们将不胜感激。
[编辑]
尝试使用查找命令
- hosts: myhost
tasks:
- name: find the file
find:
paths: /home/
patterns: '*.iso'
register: prod
- debug: var=item.path
with_items: "{{ prod.files }}"
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ prod.files }}"
validate_certs: false
此外,在 community.vmware.vsphere_copy 模块下定义的“路径”中,我只需要名称 test.iso 而不是整个路径 /home/test.iso
调试输出:
[myhost] => (item={'path': '/home/linux.iso', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 1295247360, 'inode': 134527790, 'dev': 64776, 'nlink': 1, 'atime': 1648020499.1899607, 'mtime': 1648013066.2809734, 'ctime': 1648020450.6790545, 'gr_name': 'root', 'pw_name': 'root', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}) => {
"ansible_loop_var": "item",
"item": {
"atime": 1648020499.1899607,
"ctime": 1648020450.6790545,
"dev": 64776,
"gid": 0,
"gr_name": "root",
"inode": 134527790,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1648013066.2809734,
"nlink": 1,
"path": "/home/linux.iso",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 1295247360,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"item.path": "/home/linux.iso"
}
遍历 prod.files,获取路径键并使用基本名称过滤器:
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ item.path | basename }}"
validate_certs: false
loop: "{{ prod.files }}"
我在 ansible 中遇到 stdout.lines 的问题。有没有其他选择? 这是我的代码
- hosts: myhost
tasks:
- name: get contents
shell: "ls /home/*.iso | cut -d '/' -f 7 "
register: prod
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ prod.stdout_lines }}"
validate_certs: false
当我 运行 这个时,我在 UI 中得到的输出是
['test.iso']
但预期的输出是
test.iso
如有任何帮助,我们将不胜感激。
[编辑] 尝试使用查找命令
- hosts: myhost
tasks:
- name: find the file
find:
paths: /home/
patterns: '*.iso'
register: prod
- debug: var=item.path
with_items: "{{ prod.files }}"
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ prod.files }}"
validate_certs: false
此外,在 community.vmware.vsphere_copy 模块下定义的“路径”中,我只需要名称 test.iso 而不是整个路径 /home/test.iso
调试输出:
[myhost] => (item={'path': '/home/linux.iso', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 1295247360, 'inode': 134527790, 'dev': 64776, 'nlink': 1, 'atime': 1648020499.1899607, 'mtime': 1648013066.2809734, 'ctime': 1648020450.6790545, 'gr_name': 'root', 'pw_name': 'root', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}) => {
"ansible_loop_var": "item",
"item": {
"atime": 1648020499.1899607,
"ctime": 1648020450.6790545,
"dev": 64776,
"gid": 0,
"gr_name": "root",
"inode": 134527790,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1648013066.2809734,
"nlink": 1,
"path": "/home/linux.iso",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 1295247360,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
"item.path": "/home/linux.iso"
}
遍历 prod.files,获取路径键并使用基本名称过滤器:
- name: Copy
community.vmware.vsphere_copy:
hostname: host
username: myuser
password: mypass
src: "{{ my_path }}"
datacenter: VMFStorage
datastore: SAN-0
path: "/test_path/{{ item.path | basename }}"
validate_certs: false
loop: "{{ prod.files }}"