首页 > 解决方案 > 无法将文件从主机复制到 ansible 服务器

问题描述

我正在尝试用ansible做一个简单的过程,但是在尝试运行这个剧本时我失败了,我只想把用户临时目录中的现有文件复制回里面的ansible服务器etc/ansible/files

路径和权限

root@ansible:/etc/ansible/files# pwd
/etc/ansible/files
root@ansible:/etc/ansible/files# ls -ltr ../
total 24
-rw-r--r-- 1 root root  535 mar 27 11:23 ansible.cfg
-rw-r--r-- 1 root root  188 mar 27 15:41 hosts
drwxr-xr-x 5 root root 4096 mar 27 15:42 roles
drwxr-xr-x 2 root root 4096 mar 27 15:42 group_vars
drwxrwxrwx 2 root root 4096 mar 27 16:59 files
drwxr-xr-x 3 root root 4096 mar 27 17:01 playbook

剧本

- name: auto_collect_pingprobe
  hosts: "{{ affected_host }}"
  gather_facts: no
  tasks:
    - block:

        - name: 'Copy net connect'
          fetch:
            src: '%temp%\net_connect.cfg'
            dest: '/etc/ansible/files/net_connect.cfg'
            flat: yes

      rescue:
        - fail:
            msg: "Failure detected in playbook"

输出

fatal: [192.168.238.12]: FAILED! => {
    "msg": "failed to transfer file to \"/etc/ansible/files/net_connect.cfg\""
}

TASK [fail] *************************************************************************************************************************************************
task path: /etc/ansible/playbook/GEN_AUTO_COLLECT_HOST_AVAILABLE.yml:22
fatal: [192.168.238.12]: FAILED! => {
    "changed": false,
    "msg": "Failure detected in playbook"
}

标签: pythonansible

解决方案


可能会发生两件事。

  1. 您可能在受控节点上没有 root 权限,请确保您--become在调用笔记本时使用该标志(或使用等效的权限提升)。
  2. %temp%变量可能不是从环境中读取的。尝试将src字符串替换为'{{ lookup("env", "temp") }}\net_connect.cfg'.

推荐阅读