O
O
O Di2016-01-20 15:08:21
Ansible
O Di, 2016-01-20 15:08:21

How else to intercept the created file?

Essence - I want to take away a certain amount of lines from a file in a new file.

---
  - 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

The file is being created, it is not possible to copy it using another task in the playbook:
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
O Di, 2016-01-20
@insiki

I answer my own question:
The description of the Copy module as be hints that this module copies from local to remote :)
You must use the command or shell module:

- name: Move foo to bar
  command: creates="path/to/bar" cp /path/to/foo /path/to/bar

Or, if you have Ansible 2.0, use the same Copy module, but with the remote_src=True option enabled:
- name: Copy files from foo to bar
  copy: remote_src=True src=/path/to/foo dest=/path/to/bar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question