我有几个ansible
剧本,有时在本地环境中有意义,否则它们是远程执行的。为了做到这一点我用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
Exec的本地翻番:
$ 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
Exec的遥控器:
$ 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
。
会起作用:'connection:“{{'ansible_host'| default('local')}}”'? [Docs](http://docs.ansible.com/ansible/intro_inventory.html#non-ssh-connection-types)似乎不明确,'ansible_host'被定义为'要连接到的Docker容器的名称'。我愿意这只是文档中的错误。 –
我会在清单文件中设置连接,如本节底部所述:http://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups – udondan
'localhost ansible_connection = local' – udondan