Browse Source

serialization, parallelism, async tasks

Grega Bremec 3 months ago
parent
commit
c61a80d9ab
4 changed files with 84 additions and 0 deletions
  1. 9 0
      ansible.cfg
  2. 23 0
      do-something.yml
  3. 7 0
      hosts
  4. 45 0
      reboot.yml

+ 9 - 0
ansible.cfg

@@ -0,0 +1,9 @@
+[defaults]
+remote_user = devops
+inventory = ./hosts
+nocows = yes
+forks = 10
+
+[privilege_escalation]
+become_method = sudo
+become_ask_pass = no

+ 23 - 0
do-something.yml

@@ -0,0 +1,23 @@
+---
+- name: bogus
+  hosts: servers
+  gather_facts: no
+  #order: shuffle
+  max_fail_percentage: 34%
+  serial:
+    - 50%
+    - 100%
+  tasks:
+    - name: create a directory /tmp/foo
+      file:
+        path: /tmp/foo
+        state: absent
+
+#    - name: just pretend
+#      #run_once: yes
+#      command: sleep 1
+#      changed_when: false
+
+    - name: pretend some more
+      command: echo foo
+      changed_when: false

+ 7 - 0
hosts

@@ -0,0 +1,7 @@
+[servers]
+servera.lab.example.com
+serverb.lab.example.com
+serverc.lab.example.com
+serverd.lab.example.com
+bastion.lab.example.com ansible_user=root
+workstation.lab.example.com

+ 45 - 0
reboot.yml

@@ -0,0 +1,45 @@
+---
+- name: reboot a machine
+  #hosts: serverd.lab.example.com
+  hosts: localhost
+  gather_facts: no
+  tasks:
+    - name: reboot
+      command: sleep 30
+      changed_when: false
+      async: 125
+      poll: 0
+      register: long_runner
+
+#    - name: reboot
+#      delegate_to: serverd.lab.example.com
+#      become: yes
+#      reboot:
+#        reboot_timeout: 30
+
+    - name: do some jiggawagoo
+      command: sleep 3
+
+    - debug: var=long_runner
+
+    - name: check if the long-running task completed yet
+      async_status:
+        jid: "{{ long_runner.ansible_job_id }}"
+      register: long_check
+      until: long_check.finished
+      delay: 5
+      retries: 20
+
+#    - name: now wait for serverd to complete before continuing
+#      delegate_to: serverd.lab.example.com
+#      wait_for:
+#        port: 22
+#        delay: 30
+
+#    - name: get uptime
+#      delegate_to: serverd.lab.example.com
+#      command: uptime
+#      register: uptime
+
+#    - debug: var=uptime
+