【问题标题】:ansible playbook run with play tag gathers facts even that gathering facts set to 'no'使用 play 标签运行的 ansible playbook 收集事实,即使收集事实设置为“否”
【发布时间】:2022-01-12 12:35:33
【问题描述】:

ansible-playbook 下面使用ansible-playbook playbook.yml --tags=rancher 运行

- name: instal docker 
  hosts: rancher-server
  become: yes
  gather_facts: yes

  roles:
    - role: some_galaxy_role

- name: install rancher 
  hosts: rancher-server
  become: yes
  gather_facts: no
  tasks:
    - name: install rancher
      debug:
  tags:
    - rancher

只有install rancher 播放被rancher 标签选中并按预期运行。然而,第一场比赛install docker 的事实收集仍然需要时间。为什么以及有没有办法避免它?

下面是 playbook 运行的输出:

PLAY [install docker] *********************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************
ok: [rancher-server1]
ok: [rancher-server2]


PLAY [install rancher] 

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您可以输入a tag on the play level,从而跳过整个Instal Docker 播放。

    给定:

    - name: Install Docker
      hosts: localhost
      gather_facts: yes
      tags:
        - docker
    
      tasks:
        - debug:
    
    - name: Install rancher
      hosts: localhost
      gather_facts: yes
      tags:
        - rancher
    
      tasks:
        - debug:
    

    当使用--tags rancher 运行时,会产生:

    PLAY [Install Docker] *********************************************************************************************
    
    PLAY [Install rancher] ********************************************************************************************
    
    TASK [Gathering Facts] ********************************************************************************************
    ok: [localhost]
    
    TASK [debug] ******************************************************************************************************
    ok: [localhost] => 
      msg: Hello world!
    
    PLAY RECAP ********************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
    

    另一方面,请注意不要强迫您收集所有事实,you can also gather subsets,以加快播放速度。

    例如,您只能使用事实的最小子集:

    - name: Install Docker
      hosts: localhost
      gather_subset:
        - min
    
      tasks:
        - debug:
    

    当然,这完全取决于some_galaxy_role 中需要您收集事实的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2015-09-12
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 2014-09-25
      相关资源
      最近更新 更多