1

I want to be able to write one playbook to update all my servers in different environments - stage and prod. I do not want to reboot these servers every time there is an update to the kernel, I only want to reboot for example every 2 months.

Is there a way to pass a reboot variable to ansible command to trigger reboot of all servers only when I want to reboot?

I will be using Jenkins to trigger the update job.

Sample code below:

- hosts: all
  become: yes
  tasks:  
    - name: Making changes to the yum.conf file
      shell: sudo sed "s/exclude=kernel/#exclude=kernel/g" /etc/yum.conf
      args:
        executable: /bin/bash

    - name: Performing yum update
      yum:
        update_cache: yes
        name: '*'
        state: latest
        update_cache: yes

    - name: Checking for reboot
      shell: LAST_KERNEL=$(rpm -q --last kernel | awk 'NR==1{sub(/kernel-/,""); print $1}'); CURRENT_KERNEL=$(uname -r); if [ $LAST_KERNEL != $CURRENT_KERNEL ]; then echo 'reboot'; else echo 'no'; fi
      ignore_errors: true
      register: reboot_hint

    - name: Rebooting servers now ...
      command: shutdown -r now "Reboot required for updated kernel"
      async: 0
      poll: 0
      sudo: true
      ignore_errors: true
      when: reboot_hint.stdout.find("reboot") != -1
      register: rebooting

    - name: Taking a nap while servers reboot...
      pause: seconds=200
      when: rebooting is changed

    - name: Confirming servers are back online
      wait_for:
        host: "{{ ansible_ssh_host | default(inventory_hostname) }}"
        delay: 30
        state: started
        search_regex: OpenSSH
        port: 22
      become: false
      when: reboot is changed
      delegate_to: localhost
2

You can pass extra variables to ansible playbooks by running

ansible-playbook --limit whatever myplaybook.yml --extra-vars reboot=now

Modify the top of your playbook:

- hosts: all
  become: yes
  vars:
    reboot: notnow

The reboot task becomes:

- name: Rebooting servers now ...
  command: shutdown -r now "Reboot required for updated kernel"
  async: 0
  poll: 0
  sudo: true
  ignore_errors: true
  when: reboot == "now"
  register: rebooting

When you don't pass the extra-vars parameter then the var has the value "notnow" and then when condition won't be satisfied.

  • This is really helpful. Thanks a lot. One question tho (best practice question) - my stage and prod environment is basically identical (consists of mostly windows db servers, linux app, linux web servers) do you think it's best practice to have them grouped by env & application then drill down to each application and it's db, web,app servers or I should just stick to one host file with [stage] and [prod] ? – bonafideaf May 29 '18 at 0:55
  • That really depends on how you are going to use ansible to orchestrate your servers. Of course there's nothing wrong with putting servers into multiple groups and selecting according to what you're doing. – wurtel May 29 '18 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.