在ansible playbook中选择主机

时间:2017-03-22 11:00:33

标签: ansible

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

我有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]

1 个答案:

答案 0 :(得分:0)

你应该尝试魔术变量(http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts

- 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