我想根据条件在特定主机上运行命令。
我有ansible hosts条目
[myhost_list]
myfqdn1.net 156.65.235.23
myfqdn2.net 157.23.36.3
myfqdn3.net 65.23.36.3
情景#1
选择上面列表中的第一个主机并运行一些命令
e.g., Run some command on 156.65.235.23
情景#2
选择上面列表中除第一个主机以外的剩余主机并运行一些命令
e.g Run some command on 157.23.36.3, 65.23.36.3
我写过以下剧本但没有运气,我试图设置主持人名称
- hosts: myhost_list
tasks:
- name: set host specific command
command: #some command#
when: ansible_ssh_host == groups['myhost_list'][0]
- name: set host specific command
command: #some command#
when: ansible_ssh_host == groups['myhost_list'][1]
- name: set host specific command
command: #some command#
when: ansible_ssh_host == groups['myhost_list'][2]
答案 0 :(得分:0)
- name: set host specific command
command: #some command#
when: ansible_ssh_host in group_names
- name: set host specific command
command: #some command#
when: ansible_ssh_host in group_names
- name: set host specific command
command: #some command#
when: ansible_ssh_host in groups