我想根据条件在特定主机上运行命令 .
我有ansible主机入口
[myhost_list]
myfqdn1.net 156.65.235.23
myfqdn2.net 157.23.36.3
myfqdn3.net 65.23.36.3
Scenario #1
选择上面列表中的第一个主机并运行一些命令
e.g., Run some command on 156.65.235.23
Scenario #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]
1 回答
你应该尝试魔术变量(http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)