在ansible playbook中选择主机

在ansible playbook中选择主机,ansible,Ansible,我想根据条件在特定主机上运行命令 我有一份工作要做 [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 e.g Run some command on 157.23.36.3, 65.23.3

我想根据条件在特定主机上运行命令

我有一份工作要做

 [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
e.g Run some command on  157.23.36.3, 65.23.36.3  
场景#2
在上面的列表中选择除第一个主机之外的其余主机,然后运行一些命令

e.g., Run some command on 156.65.235.23
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]

你应该试试神奇的变量()


很抱歉拼写错误,现在我已更正了它的名称而不是主机名。我无法使用when条件选择主机。请允许我尝试您的建议