Ansible-同一主机的多个/备用主机名

时间:2019-01-15 12:42:33

标签: ansible

假设我的主机具有多个(DNS)名称/ IP,例如因为它们具有多个NIC,并因此具有到达它们的路由。

如果这些路线之一失败,我想播放剧本。由于我不知道哪个作品可行,因此我想尝试所有这些作品,然后仅一次为该主持人播放。将所有主机名放入清单中并使其运行很容易,但随后对每个主机名执行一次剧本。

问题:是否可以指定备用主机名或告诉ansible每个组仅在一台主机上运行剧本?

1 个答案:

答案 0 :(得分:1)

可以实现

  

每组只能在一台主机上运行剧本

请参见下面的示例。

- hosts: jails-01                                                                            
  strategy: linear                                                                           
  vars:                                                                                      
    lock_file: /var/lock/my_ansible_hostname.lock                                                 
  tasks:                                                                                     
    - name: delete lock_file                                                                 
      file:                                                                                  
        path: "{{ lock_file }}"                                                              
        state: absent                                                                        
      run_once: true                                                                         
      delegate_to: localhost                                                                 
    - name: select host                                                                      
      shell: "echo {{ ansible_hostname }}  > {{ lock_file }}"                                
      args:                                                                                  
        creates: "{{ lock_file }}"                                                           
      delegate_to: localhost                                                                 
    - name: winner takes it all                                                              
      fail:                                                                                  
        msg: "Too late. Other thread is running. End of play."                               
      when: lookup('file', lock_file) != ansible_hostname                                    
    - name: go ahead                                                                         
      debug:                                                                                 
        msg: "{{ ansible_hostname }} goes ahead ... "


# ansible-playbook playbook.yml | grep msg
fatal: [test_01]: FAILED! => {"changed": false, "msg": "Too late. Other thread is running. End of play."}
fatal: [test_03]: FAILED! => {"changed": false, "msg": "Too late. Other thread is running. End of play."}
    "msg": "test_02 goes ahead ... "