Ansible - 检查本地是否存在多个文件并复制到远程
Ansible - Check if multiple files exists locally and copy to remote
我是 Ansible 的新手,我正在尝试检查我的 Ansible 控制机器中是否存在文件(至少应该有一个),如果存在,请复制到远程位置。
我想出了以下方法,但 ansible 正在检查远程而不是本地中的文件。我不太确定 - 如何使用 "with_items"。
---
- hosts: linux
gather_facts: no
vars:
source_dir: /login/my_home
server_type: WRITEVIEW
files:
- app.xslt
- Configuration.xml
- fcCN.xslt
tasks:
- name: Validate if file exists
local_action: file path="{{ source_dir }}/{{ item }}" state=file
with_items: files
错误信息:
TASK [Validate if file exists] ******************************************************************************************************************************************
failed: [remoteserver_1 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"}
failed: [remoteserver_2 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"}
您可以使用stat
命令来检查文件是否存在。如果存在,那么copy
它到远程服务器:
---
- hosts: linux
gather_facts: no
vars:
source_dir: /login/my_home
server_type: WRITEVIEW
files:
- app.xslt
- Configuration.xml
- fcCN.xslt
tasks:
- name: check if file exists
local_action: stat path=/path/of/file/{{ item }}
with_items: "{{ files }}"
register: check_file_name
- name: Verifying if file exists
debug: msg="File {{ item.item }} exist"
with_items: "{{ check_file_name.results }}"
when: item.stat.exists
- name: Copying the file
copy: src=/path/of/file/local/{{ item.item }} dest=/path/of/file/remote/
with_items: "{{ check_file_name.results }}"
when: item.stat.exists
我是 Ansible 的新手,我正在尝试检查我的 Ansible 控制机器中是否存在文件(至少应该有一个),如果存在,请复制到远程位置。
我想出了以下方法,但 ansible 正在检查远程而不是本地中的文件。我不太确定 - 如何使用 "with_items"。
---
- hosts: linux
gather_facts: no
vars:
source_dir: /login/my_home
server_type: WRITEVIEW
files:
- app.xslt
- Configuration.xml
- fcCN.xslt
tasks:
- name: Validate if file exists
local_action: file path="{{ source_dir }}/{{ item }}" state=file
with_items: files
错误信息:
TASK [Validate if file exists] ******************************************************************************************************************************************
failed: [remoteserver_1 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"}
failed: [remoteserver_2 -> localhost] (item=files) => {"changed": false, "item": "files", "msg": "file (/login/my_home/files) is absent, cannot continue", "path": "/login/my_home/files", "state": "absent"}
您可以使用stat
命令来检查文件是否存在。如果存在,那么copy
它到远程服务器:
---
- hosts: linux
gather_facts: no
vars:
source_dir: /login/my_home
server_type: WRITEVIEW
files:
- app.xslt
- Configuration.xml
- fcCN.xslt
tasks:
- name: check if file exists
local_action: stat path=/path/of/file/{{ item }}
with_items: "{{ files }}"
register: check_file_name
- name: Verifying if file exists
debug: msg="File {{ item.item }} exist"
with_items: "{{ check_file_name.results }}"
when: item.stat.exists
- name: Copying the file
copy: src=/path/of/file/local/{{ item.item }} dest=/path/of/file/remote/
with_items: "{{ check_file_name.results }}"
when: item.stat.exists