gpt4 book ai didi

ansible - 如何根据远程主机在本地主机上执行命令

转载 作者:行者123 更新时间:2023-12-02 20:55:00 27 4
gpt4 key购买 nike

在我的剧本中我有这个:

- name: compile 
hosts: localhost
gather_facts: false
tasks:
- name: compile binary
local_action: command make build FOO=foo1

如果主机是 bar-1bar-2,我想在本地主机上执行 make build FOO=bar1 一次 (它们都在 bars 组中,因此按组区分也可以)。我尝试使用 when:

  - name: compile binary
local_action: command make build FOO=foo1
when: (inventory_hostname != "bar-1") and (inventory_hostname != "bar-2")
- name: compile binary
local_action: command make build FOO=bar1
when: (inventory_hostname == "bar-1") or (inventory_hostname == "bar-2")

但是inventory_hostname始终是localhost

在我的主机中我有

[foos]
foo-1 ...
foo-2 ...

[bars]
bar-1 ...
bar-2 ...

我将其运行为

ansible-playbook -i provision/hosts -l localhost,bars provision/deploy.yml

最佳答案

这对我来说效果很好:

---
- hosts: localhost,test-server
gather_facts: no
tasks:
- shell: echo {{ inventory_hostname }}
delegate_to: localhost

命令在本地主机上执行,但打印localhosttest-server

关于ansible - 如何根据远程主机在本地主机上执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com