Запустите Ansible Task на локальном хосте - PullRequest
1 голос
/ 28 октября 2019

Мне нужно проверить, существует ли один файл (/tmp/test.html) на локальном хосте, и, если он существует, выполнить другие задачи. Не могли бы вы помочь запустить это первое задание (имя: Проверить существование и скопировать) в localhost (рабочая станция).

localhost: рабочая станция remotehost: servera, serverb

Ниже приведен мой playbook.yml

---
- name: Check exist and copy
  hosts: all
  tasks:
  - name: check if file is exists #need to execute this task in workstation
    stat: 
     path: /tmp/test.html
    register: file_present

  - name: copy to taggroup 1
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest1.html
    when: file_present.stat.exists == 0 and inventory_hostname in groups ['taggroup1']

  - name: copy to taggroup 2
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest2.html
    when: file_present.stat.exists == 0 and inventory_hostname in groups ['taggroup2']

Ответы [ 4 ]

2 голосов
/ 28 октября 2019

Модуль stat не требуется, когда пути проверяются на локальном хосте . Например, fail воспроизведение, если файл / tmp / test.html не существует, и продолжить воспроизведение в противном случае.

- hosts: all
  vars:
    my_file: '/tmp/test.html'
  tasks:
    - fail:
        msg: "{{ my_file }} does not exist. End of play."
      when: my_file is not exists
      delegate_to: localhost
      run_once: true

    - debug:
        msg: "Continue play."
      run_once: true
0 голосов
/ 29 октября 2019

Спасибо всем за отличную поддержку. Это фиксированный ответ.

---
- name: Check exist and copy
  hosts: all
  tasks:
  - name: check if file is exists #need to execute this task in workstation
    stat: 
     path: /tmp/test.html
    register: file_present
    delegate_to: localhost
    run_once_ true

  - name: copy to taggroup 1
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest1.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup1']

  - name: copy to taggroup 2
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest2.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup2']


0 голосов
/ 28 октября 2019

Попробуйте следовать

---

- name: Check exist and copy
  hosts: all
  tasks:
  - name: check if file is exists #need to execute this task in workstation
      stat: path=/tmp/test.html
    register: file_present
    delegate_to: localhost 

  - name: copy to taggroup 1
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest1.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup1']

  - name: copy to taggroup 2
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest2.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup2']
0 голосов
/ 28 октября 2019

Попробуйте следующее:

---
- name: Check exist and copy
  hosts: all
  tasks:
  - name: check if file is exists #need to execute this task in workstation
    stat: 
     path: /tmp/test.html
    register: file_present
    delegate_to: localhost

  - name: copy to taggroup 1
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest1.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup1']

  - name: copy to taggroup 2
    copy: 
     src: /tmp/test.html 
     dest: /tmp/dest2.html
    when: file_present.stat.exists and inventory_hostname in groups ['taggroup2']

См. документы .

Вы можете использовать delegate_to: для запуска задачи на компьютере, отличном от текущего ansible_host.

...