Ansible:如何在特定主机中运行角色
我有一个剧本,它调用多个角色并在多个主机中执行它们: 我的剧本:Ansible:如何在特定主机中运行角色,ansible,ansible-2.x,ansible-inventory,ansible-facts,Ansible,Ansible 2.x,Ansible Inventory,Ansible Facts,我有一个剧本,它调用多个角色并在多个主机中执行它们: 我的剧本: --- - hosts: all gather_facts: true vars: - selected_APIS: "{{ RCD_APIS.split(',') }}" pre_tasks: - name : Display selected micro-services run_once: true debug: msg: "{{selected_APIS}
---
- hosts: all
gather_facts: true
vars:
- selected_APIS: "{{ RCD_APIS.split(',') }}"
pre_tasks:
- name : Display selected micro-services
run_once: true
debug:
msg: "{{selected_APIS}}"
roles:
- { role: pullDockerImages , when: '"PULL" in DEPL_MODE'}
- { role: stopDockerContainers , when: '"STOP" in DEPL_MODE'}
- { role: pullDockerConFiles , when: '"START" in DEPL_MODE'} // THIS ROLE
- { role: prepareDirectoriesTree , when: '"START" in DEPL_MODE'}
- { role: startDockerContainers , when: '"START" in DEPL_MODE'}
我的目的是:
我只想在localhost或特定的一台主机上运行第三个角色
我该怎么做
我曾尝试将“hosts:localhost”
添加到该角色的任务中,但失败了,我还尝试了将任务委托给:localhost
和本地操作
,但都失败了
建议?您需要列出清单文件中的所有主机,例如本地主机
[sandbox]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh
other2.example.com ansible_connection=ssh
然后在你的剧本中,你需要通过索引引用localhost,在我的例子中,localhost有一个索引0,这就是为什么我们可以这样写
---
- hosts: all
gather_facts: true
vars:
- selected_APIS: "{{ RCD_APIS.split(',') }}"
pre_tasks:
- name : Display selected micro-services
run_once: true
debug:
msg: "{{selected_APIS}}"
roles:
- { role: pullDockerImages , when: '"PULL" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: stopDockerContainers , when: '"STOP" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: pullDockerConFiles , when: '"START" in DEPL_MODE and inventory_hostname == play_hosts[0]'} // THIS ROLE
- { role: prepareDirectoriesTree , when: '"START" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: startDockerContainers , when: '"START" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
因此,第三个角色将仅在本地主机上运行,其他角色的非本地主机将不在本地主机上运行。您需要在清单文件中列出所有主机,例如本地主机
[sandbox]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh
other2.example.com ansible_connection=ssh
然后在你的剧本中,你需要通过索引引用localhost,在我的例子中,localhost有一个索引0,这就是为什么我们可以这样写
---
- hosts: all
gather_facts: true
vars:
- selected_APIS: "{{ RCD_APIS.split(',') }}"
pre_tasks:
- name : Display selected micro-services
run_once: true
debug:
msg: "{{selected_APIS}}"
roles:
- { role: pullDockerImages , when: '"PULL" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: stopDockerContainers , when: '"STOP" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: pullDockerConFiles , when: '"START" in DEPL_MODE and inventory_hostname == play_hosts[0]'} // THIS ROLE
- { role: prepareDirectoriesTree , when: '"START" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
- { role: startDockerContainers , when: '"START" in DEPL_MODE and inventory_hostname != play_hosts[0]'}
因此,第三个角色将仅在本地主机上运行,其他角色的非角色将不会在本地主机上运行。用例是什么?好像是XY问题我现在就试过了,效果很好。你到底有什么问题?您是否收到被拒绝的
权限
?如果是这样的话,请更新问题,我也许能帮上忙。@ThomasBöhm你到底喜欢什么?@firasKoubaa在剧本中使用localhost
作为hosts:
的参数。您可以(在上面发布的代码中)将hosts:all
更改为hosts:localhost
用例是什么?好像是XY问题我现在就试过了,效果很好。你到底有什么问题?您是否收到被拒绝的权限
?如果是这样的话,请更新问题,我也许能帮上忙。@ThomasBöhm你到底喜欢什么?@firasKoubaa在剧本中使用localhost
作为hosts:
的参数。您可以(在上面发布的代码中)将hosts:all
更改为hosts:localhost