首页 > 解决方案 > Ansible:将文件从主机复制到网络机器

问题描述

我尝试使用以下命令行使用 ansible 复制文件,但出现 creds 错误。

ansible -i ./hosts all -m copy -a "src=/etc/hosts dest=/tmp/hosts" -k

SSH 密码:

192.168.0.166 | 遥不可及!=> { "changed": false, "msg": "无效/错误密码:权限被拒绝,请重试。", "unreachable": true

但是,我在运行 ping 时得到了正确的输出。

ansible -i ./hosts all -u ansible -m ping

192.168.0.122 | 成功 => {“改变”:假,“平”:“乒乓”

ssh 公钥肯定会复制到服务器中。据我检查,linux文件夹中的权限没问题。我需要用这条线运行它,而不是使用剧本。你有什么主意吗 ?我需要指定用户吗?

标签: linuxansiblecopy

解决方案


像这样创建主机文件

[root@chfapp directory]# cat host
[remote]
10.x.x.x

创建无密码 SSH

ssh-copy-id root@10.x.x.x

然后使用此命令进行 ping

[root@chfapp directory]# ansible -i host all -m ping
10.x.x.x | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": false, 
    "ping": "pong"
}

如果你得到 pong 然后只尝试复制命令

[root@chfapp directory]# ansible -i ./host all -m copy -a "src=/etc/hosts dest=/tmp/hosts" -k
SSH password: 
10.x.x.x | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    }, 
    "changed": true, 
    "checksum": "afc8b584b5d97582547c5ec261c5e407b551d06a", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "1316425243d92c3ddbadda6096d1d105", 
    "mode": "0644", 
    "owner": "root", 
    "size": 245, 
    "src": "/root/.ansible/tmp/ansible-tmp-1616767392.68-28343-81423044115547/source", 
    "state": "file", 
    "uid": 0
}

推荐阅读