---
- hosts: test
gather_facts: no
remote_user: username
sudo: yes
vars:
- incoming_dir: /opt/incoming
- names_dir: /opt/names
- src_names: /opt/names/src.names
- names_count: 100000
- timestamp: "{{ lookup('pipe', 'date +%Y%m%d%H%M') }}"
- new_names: "{{ names_dir }}/{{ names_count }}-{{ timestamp }}.names"
tasks:
- name: "generate new names file"
shell: head -n {{ names_count }} {{ src_names }} >> {{ new_names }}
register: names_created
- debug: var=new_names
- name: "cp new names to incoming/ directory"
copy: src={{ new_names }} dest={{ incoming_dir }}/{{ new_names | basename }} owner=username group=username mode=0644
when: names_created
PLAY [test] *********************************************************
TASK: [generate new names file] ***********************************************
changed: [test]
TASK: [debug var=new_names] ***************************************************
ok: [test] => {
"var": {
"new_names": "/opt/names/100000-201601201505.names"
}
}
TASK: [file path={{ new_names }} owner=username group=username mode=0644] ***
changed: [test]
TASK: [cp new names to incoming/ directory] ***********************************
failed: [test] => {"failed": true}
msg: could not find src=/opt/names/100000-201601201505.names
FATAL: all hosts have already failed -- aborting
The copy module copies a file on the local box to remote locations. Use the fetch module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the template module.
should use the command module to do a cp in this case.
- name: Move foo to bar
command: creates="path/to/bar" cp /path/to/foo /path/to/bar
From version 2.0 in file plugin you can use remote_src parameter. If True it will go to the remote/target machine for the src.
- name: Copy files from foo to bar
copy: remote_src=True src=/path/to/foo dest=/path/to/bar