insiki
@insiki
broken pipe

Как по-другому перехватить созданный файл?

Суть - хочу из файла забирать определенное количество строк в новый файл.
---
  - 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
  • Вопрос задан
  • 233 просмотра
Решения вопроса 1
insiki
@insiki Автор вопроса
broken pipe
Отвечаю на свой вопрос:
Описание модуля Copy как бэ намекает, что этот модуль копирует с локальной на удаленную :)
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.

Необходимо использовать модуль command или shell:
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

Либо, если у вас Ansible 2.0, использовать всё тот же модуль Copy, но с включенной опцией remote_src=True:
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
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы