在本地主机中运行Ansible任务
我需要检查localhost上是否存在一个文件(/tmp/test.html),如果存在,则执行其他任务。 请您帮助在本地主机(工作站)中运行第一个任务(名称:检查存在和复制) 本地主机:工作站 远程主机:服务器A、服务器B 下面是我的剧本在本地主机中运行Ansible任务,ansible,ansible-inventory,Ansible,Ansible Inventory,我需要检查localhost上是否存在一个文件(/tmp/test.html),如果存在,则执行其他任务。 请您帮助在本地主机(工作站)中运行第一个任务(名称:检查存在和复制) 本地主机:工作站 远程主机:服务器A、服务器B 下面是我的剧本 --- - name: Check exist and copy hosts: all tasks: - name: check if file is exists #need to execute this task in workstatio
---
- 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']
看
您可以使用delegate\u to:
在不同于当前ansible\u主机的机器上运行任务,在以下情况下不需要模块stat。例如,如果文件/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']
请注意,使用上述手册,委派任务将针对all
组中的每个主机运行(例如,如果您有100台主机,它将运行100次)。您可能希望查看和,以便只运行一次,并为localhost存储事实。您在when
子句中的测试将变成:hostvars['localhost'].file\u present.stat.exists
与您之前1小时发布的Calum Halpin的答案有什么区别?对我来说这看起来像是一个绝对的复制品。。。