Ansible有条件地将_委托给本地还是远程?

Ansible有条件地将_委托给本地还是远程?,ansible,ansible-playbook,Ansible,Ansible Playbook,我有一些ansible剧本,它们有时在本地环境中有意义,否则它们是远程执行的。为了做到这一点,我使用delegate_to指令,但这也意味着我必须将所有任务加倍,例如: --- - hosts: all gather_facts: no tasks: - name: Local command command: hostname register: target_host when: vhost is undefined delegate_to:

我有一些
ansible
剧本,它们有时在本地环境中有意义,否则它们是远程执行的。为了做到这一点,我使用
delegate_to
指令,但这也意味着我必须将所有任务加倍,例如:

---
- hosts: all
  gather_facts: no

  tasks:

  - name: Local command
    command: hostname
    register: target_host
    when: vhost is undefined
    delegate_to: 127.0.0.1

# ---    

  - name: Remote command
    command: hostname
    register: target_host
    when: vhost is defined
本地执行

$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
changed: [new-server -> 127.0.0.1]

TASK: [Remote command] ******************************************************** 
skipping: [new-server]

PLAY RECAP ******************************************************************** 
new-server                 : ok=1    changed=1    unreachable=0    failed=0
$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml -e vhost=y

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
skipping: [new-server]

TASK: [Remote command] ******************************************************** 
changed: [new-server]

PLAY RECAP ******************************************************************** 
new-server                 : ok=1    changed=1    unreachable=0    failed=0
远程执行

$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
changed: [new-server -> 127.0.0.1]

TASK: [Remote command] ******************************************************** 
skipping: [new-server]

PLAY RECAP ******************************************************************** 
new-server                 : ok=1    changed=1    unreachable=0    failed=0
$ ansible-playbook -i inv.d/test.ini play.d/delegate.yml -e vhost=y

PLAY [all] ******************************************************************** 

TASK: [Local command] ********************************************************* 
skipping: [new-server]

TASK: [Remote command] ******************************************************** 
changed: [new-server]

PLAY RECAP ******************************************************************** 
new-server                 : ok=1    changed=1    unreachable=0    failed=0

有没有更聪明的方法告诉ansible什么时候应该回到本地环境?目前我使用的是
ansible==1.9.2

任务中不应定义应该执行的任务。当任务始终必须在本地或相关机器(如数据库主机或路由器)上运行,而playbook本身以及大多数任务运行playbook级别上定义的主机时,委派是有意义的

但是,如果您的目标是在本地或在一组远程主机上运行整个playbook,那么您应该使用不同的清单文件或组

如果您有两个不同的资源清册文件,一个定义本地主机,另一个定义所有远程主机,然后在调用ansible时应用所需的资源清册,
-i inv.d/local
-i inv.d/remote

或者将其全部放在一个库存中并动态传递给组。在库存中,您定义了两个组:

[local]
127.0.0.1

[remote]
host-1
host-2
host-N
然后将该组作为额外变量传递给ansible:
-e“run=local”
-e“run=remote”

在剧本中,您可以动态设置
主机

---
- hosts: "{{ run | mandatory }}"
  gather_facts: no
  tasks:
    ...
在您的示例中,您似乎只使用根据
vhost
extra-var定义的单个远程主机。在这种情况下,最好的选择似乎是在hosts部分重新使用此变量,并默认为localhost

---
- hosts: "{{ vhost | default('127.0.0.1') }}"
  gather_facts: no
  tasks:
    ...
因此,如果定义了
vhost
,则整个剧本将在该主机上执行。如果未定义,则playbook将在本地运行

最后,您仍然可以在单个任务上使用
delegate\u to
选项,例如:

- name: Local AND remote command
  command: hostname
  delegate_to: "{{ '127.0.0.1' if vhost is undefined else omit }}"

要使Ansible忽略该选项,就好像它不会被定义一样。

它会工作吗:
连接:“{{'Ansible_host'| default('local')}”
?似乎不明确,
ansible\u host
被定义为要连接到的Docker容器的
名称。我愿意这只是文档中的一个错误。我会在清单文件中设置连接,如本节底部所述:
localhost ansible\u connection=local
实际上我相信我需要的是:
连接:{{{{omit | default('local')}”
只需将其设置为主机变量。
连接
是一个行为参数,不需要应用于剧本或任务。一旦在资源清册或主机变量中定义,ansible将根据其连接到的主机选择正确的连接类型。