Extract previous kernel and current kernel from the playbook

Viewed 761

Below is the ansible playbook to fetch the current kernel details, reboot the server and then append the new kernel but it is not happening as expected. It is giving same kernel version which is after reboot:

- name: Header
  lineinfile:
    path: /tmp/summary.csv
    line: "HOST,PREVIOUS_KERNEL,UPTIME,CURRENT_KERNEL"
    create: yes
  delegate_to: localhost

- name: Inventory
  lineinfile:
    path: /tmp/summary.csv
    line: "{{ ansible_fqdn }},{{ ansible_kernel }},{{ ansible_uptime_seconds }}"
  delegate_to: localhost

- name: reboot
  reboot:
    reboot_timeout: 300

- name:
  lineinfile:
    path: /tmp/summary.csv
    line: "{{ ansible_fqdn }},{{ ansible_kernel }},{{ ansible_uptime_seconds }},{{ ansible_kernel }}"
  delegate_to: localhost

ANY SUGGESTION HOW TO APPEND THE CURRENT KERNEL AFTER REBOOT IN THE REPORT WHICH CONTAINS PREVIOUS KERNEL

1 Answers

The variables you are using (e.g. ansible_kernel, ansible_xxx...) are hosts facts gathered automatically by default at the beginning of your play. If your machine is running a different kernel after reboot, you will need to gather facts again for those to be updated. You can either put your second csv task in a new play inside your playbook that will gather facts by default again or you can gather facts manually after your reboot:

- name: refresh facts as the machine rebooted
  setup:
Related