0
votes

Im trying to copy a file with the following command line with ansible, but getting error with creds.

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

SSH password:

192.168.0.166 | UNREACHABLE! => { "changed": false, "msg": "Invalid/incorrect password: Permission denied, please try again.", "unreachable": true

However I get the correct output when run a ping.

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

192.168.0.122 | SUCCESS => { "changed": false, "ping": "pong"

The ssh public key is copied into the server for sure. The permission in the linux folder as far as i checked are ok. I need to run this with this line, not using playbook. Do you have any idea ? Do i need to specify a user?

2
If the ssh key is not for the user you are logged in as, you will need to specify the user with the keyRaman Sailopal
Hi Raman, im using the root user which I used to generate the ssh. Hoy can I specify the key user in the command line ?ingzane
You have specified -u ansible in the working ping example and so just use the same for the copy?Raman Sailopal
I have added the user -u ansible and worked perfectly. Thanksingzane

2 Answers

1
votes

Create host file like this

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

create passwordless SSH

ssh-copy-id [email protected]

Then use this command for 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"
}

If then you get pong then only try for copy command

[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
}
1
votes

Seems as though the ssh key are stored on the remote machine under the "ansible" user you will therefore need to specify the user when executing ansible with:

ansible -u ansible