7

This should work but doesn't and gives the following error (below).

I've read a couple of posts on stackoverflow here and here but there doesn't seem to be a good answer that works in this case. I'm really hoping I'm just missing something dumb and I have been at this for hours so please don't mind my snark but I need to vent.

Since ansible, 2.3.0, can't do something as simple as copy/move/rename files ONLY on the remote host, I'm mean who would want to do that? And it also can't act on globs (*) (say when you don't know what files to act on), a 2 step approach seems to be the only way (that I know of) to move some files (only on the remote host). But not even this works.

migrate_rhel2centos.yml

---
- hosts: RedHat
  become: true
  become_user: root
  become_method: sudo
  vars:
    repo_dir: /etc/yum.repos.d
  tasks:
  - name: create directory
    file: path=/etc/yum.repos.d/bak/ state=directory

  - name: get repo files
    shell: "ls {{ repo_dir }}/*.repo"
    register: repo_list

 - debug: var=repo_list.stdout_lines

 - name: move repo files  
   command: "/bin/mv -f {{ item }} bak"
   args:  
     chdir: "{{repo_dir}}"
   with_items: repo_list.stdout_lines


#################################

TASK [get repo files]     

**********************************************************************
changed: [myhost]

TASK [debug]    
**********************************************************************
ok: [myhost] => {
     "repo_list.stdout_lines": [
     "/etc/yum.repos.d/centric.repo", 
     "/etc/yum.repos.d/redhat.repo", 
     "/etc/yum.repos.d/rhel-source.repo"
   ]
}

TASK [move repo files]   
*******************************************************************
failed: [myhost] (item=repo_list.stdout_lines) => {"changed": true,    "cmd": ["/bin/mv", "-f", "repo_list.stdout_lines", "bak"], "delta": "0:00:00.001945", "end": "2016-12-13 15:07:14.103823", "failed": true, "item": "repo_list.stdout_lines", "rc": 1, "start": "2016-12-13 15:07:14.101878", "stderr": "/bin/mv: cannot stat `repo_list.stdout_lines': No such file or directory", "stdout": "", "stdout_lines": [], "warnings": []}
to retry, use: --limit @/home/jimm/.ansible/migrate_rhel2centos.retry

PLAY RECAP 
********************************
myhost : ok=5    changed=1    unreachable=0    failed=1   
Community
  • 1
  • 1
user195896
  • 71
  • 1
  • 1
  • 3
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Dec 13 '16 at 21:31

3 Answers3

15

Copy has the flag remote_src

If no, it will search for src at originating/master machine.

If yes it will go to the remote/target machine for the src. Default is no.

edit: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html

Does now support recursive copying.

Alex R
  • 637
  • 1
  • 8
  • 20
3

if you want to copy file only on remote server you need to use ansible.builtin.copy module with key

remote_src: yes

Example from the dock

- name: Copy a "sudoers" file on the remote machine for editing
  ansible.builtin.copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -csf %s
0
name: copy files task
  shell: cp source/path/file destination/path/file 

This resolved my issue with coping files on remote host.

Bojan Radulovic
  • 115
  • 1
  • 1
  • 14