Applying XenServer Hotfixes with Ansible

Ansible is a very simple yet powerful alternative to Puppet and Chef. It works over plain SSH and does not require an agent on the remote nodes. This makes Ansible very handy in automating tasks on XenServer hypervisor which is essentially a vendor black box.

Below is a very hackish playbook to apply predefined XenServer hotfixes. It would be a lot cleaner if a “xen” module existed for Ansible.

# main.yml
---

- action: shell /opt/xensource/bin/xe host-list name-label={{ ansible_fqdn }} | awk '/^uuid/{print $5}'
register: hostid
changed_when: hostid|failed

- include: patch_apply.yml hotfix_id={{ item }} host_id={{ hostid.stdout }}
with_items: hotfixes

# patch_apply.yml
---

- name: Check And Apply XenServer Hotfix
action: shell /opt/xensource/bin/xe patch-list | grep -qw {{ hotfix_id }}
register: result
changed_when: result|failed
ignore_errors: True

- name: Copying {{ hotfix_id }}.zip
action: copy src={{ hotfix_id }}.zip dest=/tmp/
when: result|failed

- name: Unziping {{ hotfix_id }}.zip
action: command unzip /tmp/{{ hotfix_id }}.zip -d /tmp/ creates=/tmp/{{ hotfix_id }}.xsupdate
when: result|failed

- name: Uploading patch {{ hotfix_id }} to XenServer
action: shell /opt/xensource/bin/xe patch-upload file-name=/tmp/{{ hotfix_id }}.xsupdate
register: patch_id
when: result|failed

- name: Applying {{ hotfix_id }}
action: shell /opt/xensource/bin/xe patch-apply host-uuid={{ host_id }} uuid={{ patch_id.stdout }}
when: result|failed
notify: restart xe-toolstack

- name: Cleanup Hotfix {{ hotfix_id }} install files
action: file path="/tmp/{{ hotfix_id }}*" state=absent
when: result|failed

And the run output…

TASK:  *** 
ok: [xen1.lab]

TASK: [Check And Apply XenServer Hotfix] ************************************** 
ok: [xen1.lab] => (item=XS62E001)

TASK: [Copying XS62E001.zip] ************************************************** 
skipping: [xen1.lab] => (item=XS62E001)

TASK: [Unziping XS62E001.zip] ************************************************* 
skipping: [xen1.lab] => (item=XS62E001)

TASK: [Uploading patch XS62E001 to XenServer] ********************************* 
skipping: [xen1.lab] => (item=XS62E001)

TASK: [Applying XS62E001] ***************************************************** 
skipping: [xen1.lab] => (item=XS62E001)

TASK: [Cleanup Hotfix XS62E001 install files] ********************************* 
skipping: [xen1.lab] => (item=XS62E001)

TASK: [Check And Apply XenServer Hotfix] ************************************** 
ok: [xen1.lab] => (item=XS62E002)

TASK: [Copying XS62E002.zip] ************************************************** 
skipping: [xen1.lab] => (item=XS62E002)

TASK: [Unziping XS62E002.zip] ************************************************* 
skipping: [xen1.lab] => (item=XS62E002)

TASK: [Uploading patch XS62E002 to XenServer] ********************************* 
skipping: [xen1.lab] => (item=XS62E002)

TASK: [Applying XS62E002] ***************************************************** 
skipping: [xen1.lab] => (item=XS62E002)

TASK: [Cleanup Hotfix XS62E002 install files] ********************************* 
skipping: [xen1.lab] => (item=XS62E002)

TASK: [Check And Apply XenServer Hotfix] ************************************** 
failed: [xen1.lab] => (item=XS62E004) => {"changed": true, "cmd": "/opt/xensource/bin/xe patch-list | grep -qw XS62E004 ", "delta": "0:00:00.013016", "end": "2013-08-31 22:55:22.782162", "item": "XS62E004", "rc": 1, "start": "2013-08-31 22:55:22.769146", "stdout_lines": []}
...ignoring

TASK: [Copying XS62E004.zip] ************************************************** 
changed: [xen1.lab] => (item=XS62E004)

TASK: [Unziping XS62E004.zip] ************************************************* 
changed: [xen1.lab] => (item=XS62E004)

TASK: [Uploading patch XS62E004 to XenServer] ********************************* 
changed: [xen1.lab] => (item=XS62E004)

TASK: [Applying XS62E004] ***************************************************** 
changed: [xen1.lab] => (item=XS62E004)

TASK: [Cleanup Hotfix XS62E004 install files] ********************************* 
ok: [xen1.lab] => (item=XS62E004)

NOTIFIED: [restart xe-toolstack] ********************************************** 
changed: [xen1.lab]

Shanker Balan

Shanker Balan is a devops and infrastructure freelancer with over 14 years of industry experience in large scale Internet systems. He is available for both short term and long term projects on contract. Please use the Contact Form for any enquiry.

More Posts - Website

Follow Me:
TwitterLinkedIn

Published by

Shanker Balan

Shanker Balan is a devops and infrastructure freelancer with over 14 years of industry experience in large scale Internet systems. He is available for both short term and long term projects on contract. Please use the Contact Form for any enquiry.

Leave a Reply