运行 本地主机中的 Ansible 任务
Run Ansible Task in localhost
我需要检查本地主机上是否存在一个文件 (/tmp/test.html),如果存在则执行其他任务。
你能帮忙 运行 本地主机(工作站)中的第一个任务(名称:检查存在并复制)吗?
localhost: 工作站
远程主机: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']
尝试以下操作:
---
- 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']
见the docs。
您可以使用 delegate_to:
在与当前 ansible_host
不同的机器上 运行 任务。
paths are tested at localhost 时不需要模块 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
尝试关注
---
- 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']
感谢大家的大力支持。这是固定答案。
---
- 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']
我需要检查本地主机上是否存在一个文件 (/tmp/test.html),如果存在则执行其他任务。 你能帮忙 运行 本地主机(工作站)中的第一个任务(名称:检查存在并复制)吗?
localhost: 工作站 远程主机: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']
尝试以下操作:
---
- 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']
见the docs。
您可以使用 delegate_to:
在与当前 ansible_host
不同的机器上 运行 任务。
paths are tested at localhost 时不需要模块 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
尝试关注
---
- 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']
感谢大家的大力支持。这是固定答案。
---
- 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']