如何使用 Ansible 将文件复制到本地目录? [英] How to copy file to local directory using Ansible?

查看:23
本文介绍了如何使用 Ansible 将文件复制到本地目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个剧本来配置我的开发系统.我想将 /etc/hosts 文件从我的剧本文件"目录复制到我系统上的 /etc 目录.目前我正在做以下事情:

I'm trying to set up a playbook that will configure my development system. I'd like to copy the /etc/hosts file from my playbooks "files" directory to the /etc directory on my system. Currently I'm doing the following:

# main.yml
- hosts: all
- tasks:
    - copy: src=files/hosts
            dest=/etc/hosts
            owner=root
            group=wheel
            mode=0644
            backup=true
      become: true

# inventory
localhost   ansible_connection=local

当我运行剧本时,我收到此错误:

When I run the playbook I'm getting this error:

fatal: [localhost]: FAILED! => {... "msg": Failed to get information on remote file (/etc/hosts): MODULE FAILURE"}

我相信这是因为 copy 应该用于将文件复制到远程文件系统.那么如何将文件复制到本地管理系统呢?我做了一个谷歌搜索,一切都在谈论做前者.我在 Ansible 文档中没有看到这个问题.

I believe this is because copy is supposed to be used to copy a file to a remote file system. So how do you copy a file to your local management system? I did a Google Search and everything talks about doing the former. I didn't see this addressed in the Ansible docs.

推荐答案

你的任务没问题.

您应该将 --ask-sudo-pass 添加到 ansible-playbook 调用中.

You should add --ask-sudo-pass to the ansible-playbook call.

如果你使用 -vvv 运行,你可以看到命令以 sudo -H -S -n -u root/bin/sh -c echo BECOME-SUCCESS-somerandomstring(随后调用 Python 脚本).如果您自己执行它,您将收到 sudo: a password is required 消息.Ansible 用它自己的 Failed to get information on remote file (/etc/hosts): MODULE FAILURE 替换了这个错误消息.

If you run with -vvv you can see the command starts with sudo -H -S -n -u root /bin/sh -c echo BECOME-SUCCESS-somerandomstring (followed by a call to the Python script). If you execute it yourself, you'll get sudo: a password is required message. Ansible quite unhelpfully replaces this error message with its own Failed to get information on remote file (/etc/hosts): MODULE FAILURE.

这篇关于如何使用 Ansible 将文件复制到本地目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆