How can i release code on 50% machine in Ansibe

2019-05-11 13:10发布

we have running 100 Machines in aws with tag Name=ad_server so how can i run release code only 50% machines. Example:

 - hosts: tag_Name_ad_server
   sudo: yes
   remote_user: ubuntu

   tasks:
     - name: whatever

so how can i do this..

1条回答
放我归山
2楼-- · 2019-05-11 13:44

Option 1: serial + pause

This will use standard feature of batches for rolling updates and require some user interaction.
We add pause with prompt as a first task to wait for ENTER to be pressed at the start of every batch.
So press ENTER on the first 50%-batch and then abort execution when asked to start the second half.

- hosts: tag_Name_ad_server
  user: ubuntu
  serial: "50%"
  tasks:
    - pause: prompt="Press ENTER to run this batch"
    - shell: echo task1
    - shell: echo task2

With every playbook run serial will always choose the same servers from inventory. In this case first 50% from the list.

Option 2: form a new dynamic group

Loop through hosts in tag_Name_ad_server group and form a new 50_percent group.
Then execute your actual tasks within this new group.
No user interaction required.

- hosts: tag_Name_ad_server
  gather_facts: no
  tasks:
    - group_by: key=50_percent
      when: 100 | random > 50

- hosts: 50_percent
  user: ubuntu
  tasks:
    - shell: echo task1
    - shell: echo task2

With ever playbook run random will choose 50% random servers from the list.

Option 3: dirty trick with max_fail_percentage

If you have only one task to run, you can use max_fail_percentage: -1 to stop execution after first task of first batch.
This will make a 50%-batches, execute first task on every host of this batch and check failed servers count, as it always will be above -1 playbook will stop execution.

- hosts: tag_Name_ad_server
  user: ubuntu
  serial: "50%"
  max_fail_percentage: -1
  tasks:
    - shell: echo task1
    - shell: echo task2 # this task will never be executed with max_fail_percentage == -1
查看更多
登录 后发表回答