所以我有一组nginx服务器:
[nginx_internal_servers]
n01.local
n02.local
n03.local
我有一个预部署任务要运行。我正在运行serial:1
模式中,我只希望这个预部署任务对我以外的所有人运行。
目前我的任务是这样的,它可以在所有 nginx 服务器完美运行:
pre_tasks:
- name: Take service out of nginx upstream pools
local_action: command {{ playbook_dir }}/scripts/nginx-upstream-management.sh -s {{ item[0] }} -r {{ item[2] }} -g {{ item[1] }}
with_nested:
- groups['nginx_internal_servers']
- services_endpoints.keys()
- ansible_all_ipv4_addresses|last
有什么想法可以如何从列表中排除当前节点groups['nginx_internal_servers']
?
答案1
明白了!使用 when :)
pre_tasks:
- name: Take service out of nginx upstream pools
local_action: command {{ playbook_dir }}/scripts/nginx-upstream-management.sh -s {{ item[0] }} -r {{ item[2] }} -g {{ item[1] }}
with_nested:
- groups['nginx_internal_servers']
- services_endpoints.keys()
- ansible_all_ipv4_addresses|last
when: item[0] != inventory_hostname
答案2
通用任务版本
- name: run on all but the first host in a play
some_module: some_param=some_value
when: inventory_hostname != play_hosts[0]