如果任何文件存在,我需要将 hosts_example 或 hosts_Example 重命名为 hosts_real
- name: Playbook to Standardize Hosts
hosts: test
vars:
destpath: /etc/hosts_real
filename: [ /etc/hosts_example,/etc/hosts_Example ]
tasks:
- name: Check if file exists
stat:
path: "{{ item }}"
with_items:
- "{{ filename }}"
register: check_file_name
- debug:
msg: "{{check_file_name}}"
- name: Rename file
command: mv "{{ item }}"{{destpath}}"
with_items:
- "{{ check_file_name.results }}"
when: item.stat.exists == true
我尝试了这个,但出现了错误,无法达到预期的结果
这是错误
changed': False, 'stat': {'exists': True, 'path': '/etc/hosts_Example', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 6, 'inode': 112462077, 'dev': 2053, 'nlink': 1, 'atime': 1599813413.9216056, 'mtime': 1599804909.77824, 'ctime': 1599813399.5502446, 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False, 'blocks': 8, 'block_size': 4096, 'device_type': 0, 'readable': True, 'writeable': True, 'executable': False, 'pw_name': 'root', 'gr_name': 'root', 'checksum': '3c31c404fce438e54272bd5c7e67abdcc25e4f22', 'mimetype': 'text/plain', 'charset': 'us-ascii', 'version': '1149597246', 'attributes': ['extents'], 'attr_flags': 'e'}, 'invocation': {'module_args': {'path': '/etc/hosts_Example', 'follow': False, 'get_checksum': True, 'get_mime': True, 'get_attributes': True, 'checksum_algorithm': 'sha1', 'get_md5': None}}, 'failed': False, 'item': '/etc/hosts_Example', 'ansible_loop_var': 'item'}) => {"ansible_loop_var": "item", "changed": true, "cmd": ["mv", "{'changed': False, 'stat': {'exists': True, 'path': '/etc/hosts_Example, 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 6, 'inode': 112462077, 'dev': 2053, 'nlink': 1, 'atime': 1599813413.9216056, 'mtime': 1599804909.77824, 'ctime': 1599813399.5502446, 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False, 'blocks': 8, 'block_size': 4096, 'device_type': 0, 'readable': True, 'writeable': True, 'executable': False, 'pw_name': 'root', 'gr_name': 'root', 'checksum': '3c31c404fce438e54272bd5c7e67abdcc25e4f22', 'mimetype': 'text/plain', 'charset': 'us-ascii', 'version': '1149597246', 'attributes': ['extents'], 'attr_flags': 'e'}, 'invocation': {'module_args': {'path': '/etc/hosts_Example', 'follow': False, 'get_checksum': True, 'get_mime': True, 'get_attributes': True, 'checksum_algorithm': 'sha1', 'get_md5': None}}, 'failed': False, 'item': '/etc/hosts_Example', 'ansible_loop_var': 'item'} /etc/hosts_real"], "delta": "0:00:00.006503", "end": "2020-09-11 09:55:27.442918", "item": {"ansible_loop_var": "item", "changed": false, "failed": false, "invocation": {"module_args": {"checksum_algorithm": "sha1", "follow": false, "get_attributes": true, "get_checksum": true, "get_md5": null, "get_mime": true, "path": "/etc/hosts_Example"}}, "item": "/etc/hosts_Example", "stat": {"atime": 1599813413.9216056, "attr_flags": "e", "attributes": ["extents"], "block_size": 4096, "blocks": 8, "charset": "us-ascii", "checksum": "3c31c404fce438e54272bd5c7e67abdcc25e4f22", "ctime": 1599813399.5502446, "dev": 2053, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 112462077, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/plain", "mode": "0644", "mtime": 1599804909.77824, "nlink": 1, "path": "/etc/hosts_Example", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 6, "uid": 0, "version": "1149597246", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}}, "msg": "non-zero return code", "rc": 1, "start": "2020-09-11 09:55:27.436415", "stderr": "mv: missing destination file operand after `{\\'changed\\': False, \\'stat\\': {\\'exists\\': True, \\'path\\': \\'/etc/hosts_Example\\', \\'mode\\': \\'0644\\', \\'isdir\\': False, \\'ischr\\': False, \\'isblk\\': False, \\'isreg\\': True, \\'isfifo\\': False, \\'islnk\\': False, \\'issock\\': False, \\'uid\\': 0, \\'gid\\': 0, \\'size\\': 6, \\'inode\\': 112462077, \\'dev\\': 2053, \\'nlink\\': 1, \\'atime\\': 1599813413.9216056, \\'mtime\\': 1599804909.77824, \\'ctime\\': 1599813399.5502446, \\'wusr\\': True, \\'rusr\\': True, \\'xusr\\': False, \\'wgrp\\': False, \\'rgrp\\': True, \\'xgrp\\': False, \\'woth\\': False, \\'roth\\': True, \\'xoth\\': False, \\'isuid\\': False, \\'isgid\\': False, \\'blocks\\': 8, \\'block_size\\': 4096, \\'device_type\\': 0, \\'readable\\': True, \\'writeable\\': True, \\'executable\\': False, \\'pw_name\\': \\'root\\', \\'gr_name\\': \\'root\\', \\'checksum\\': \\'3c31c404fce438e54272bd5c7e67abdcc25e4f22\\', \\'mimetype\\': \\'text/plain\\', \\'charset\\': \\'us-ascii\\', \\'version\\': \\'1149597246\\', \\'attributes\\': [\\'extents\\'], \\'attr_flags\\': \\'e\\'}, \\'invocation\\': {\\'module_args\\': {\\'path\\': \\'/etc/hosts_Example\\', \\'follow\\': False, \\'get_checksum\\': True, \\'get_mime\\': True, \\'get_attributes\\': True, \\'checksum_algorithm\\': \\'sha1\\', \\'get_md5\\': None}}, \\'failed\\': False, \\'item\\': \\'/etc/hosts_Example\\', \\'ansible_loop_var\\': \\'item\\'} /etc/hosts_real'\nTry `mv --help' for more information.", "stderr_lines": ["mv: missing destination file operand after `{\\'changed\\': False, \\'stat\\': {\\'exists\\': True, \\'path\\': \\'/etc/hosts_Example\\', \\'mode\\': \\'0644\\', \\'isdir\\': False, \\'ischr\\': False, \\'isblk\\': False, \\'isreg\\': True, \\'isfifo\\': False, \\'islnk\\': False, \\'issock\\': False, \\'uid\\': 0, \\'gid\\': 0, \\'size\\': 6, \\'inode\\': 112462077, \\'dev\\': 2053, \\'nlink\\': 1, \\'atime\\': 1599813413.9216056, \\'mtime\\': 1599804909.77824, \\'ctime\\': 1599813399.5502446, \\'wusr\\': True, \\'rusr\\': True, \\'xusr\\': False, \\'wgrp\\': False, \\'rgrp\\': True, \\'xgrp\\': False, \\'woth\\': False, \\'roth\\': True, \\'xoth\\': False, \\'isuid\\': False, \\'isgid\\': False, \\'blocks\\': 8, \\'block_size\\': 4096, \\'device_type\\': 0, \\'readable\\': True, \\'writeable\\': True, \\'executable\\': False, \\'pw_name\\': \\'root\\', \\'gr_name\\': \\'root\\', \\'checksum\\': \\'3c31c404fce438e54272bd5c7e67abdcc25e4f22\\', \\'mimetype\\': \\'text/plain\\', \\'charset\\': \\'us-ascii\\', \\'version\\': \\'1149597246\\', \\'attributes\\': [\\'extents\\'], \\'attr_flags\\': \\'e\\'}, \\'invocation\\': {\\'module_args\\': {\\'path\\': \\'/etc/hosts_Example\\', \\'follow\\': False, \\'get_checksum\\': True, \\'get_mime\\': True, \\'get_attributes\\': True, \\'checksum_algorithm\\': \\'sha1\\', \\'get_md5\\': None}}, \\'failed\\': False, \\'item\\': \\'/etc/hosts_Example\\', \\'ansible_loop_var\\': \\'item\\'} /etc/hosts_real'", "Try `mv --help' for more information."], "stdout": "", "stdout_lines": []}
编辑1:
我修改了任务
- name: Rename file
shell: " mv {{ item }} {{destpath}} "
with_items:
- "{{ check_file_name.results }}"
when: item.stat.exists == true
我现在看到以下错误
["mv: target `/etc/hosts_real' is not a directory"]
答案1
您提供的参数太多了mv
。item
包含了很多信息,您只需要路径。请使用{{ item.stat.path }}
。
这应该可以做到:
- name: Rename file
command: mv "{{ item.stat.path }}" "{{destpath}}"
with_items:
- "{{ check_file_name.results }}"
when: item.stat.exists == true
您也遗漏了"
那一行。
另请注意,已with_items
弃用,你应该将其改为循环。