简体   繁体   中英

Compare Kernel version of 2 managed nodes

I'm working on a cluster setup and need to compare if the kernel versions of both the machines are same if not end the play using "meta" but it is not functioning as expected and giving error:

- name: Cluster setup
  hosts: cluster_nodes
  tasks:
    - name: Check kernel version of primary
      shell: uname -r
      when: inventory_hostname in groups['primary']
      register: primary

    - name: check kernel version of secondary
      shell: uname -r
      when: inventory_hostname in groups['secondary']
      register: secondary

    - meta: end_play
      when: primary.stdout != secondary.stdout

ERROR:

ERROR! The conditional check 'primary.stdout != secondary.stdout' failed. The error was: error while evaluating conditional (primary.stdout != secondary.stdout): 'dict object' has no attribute 'stdout'

The error appears to be in '/var/lib/awx/projects/pacemaker_RHEL_7_ST/main_2.yml': line 55, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


    - meta: end_play
      ^ here

Please suggest how to write a when condition to stop the play if the OS versions are not RHEL7 and both are of same kernel version.

Why firing a shell when the information you need is directly inside the gathered facts?

Moreover, your above logic is wrong as:

  • all servers in your cluster_nodes group go through all tasks which are skipped when the condition is not met (hence why you do not get a stdout defined on your register on skipped servers)
  • you are only trying to compare 2 servers (one from each primary and secondary group) where your cluster can grow and contain many. So what you want to check IMO is that all kernel version are aligned for all nodes. If this is not exactly what you want, you can still adapt from the below example.

Here is how I would do that check.

- name: Cluster setup
  hosts: cluster_nodes

  vars:
    # Get all kernel versions from all nodes into a list
    # Note that this var will be undefined if facts are not gathered
    # prior to using it.
    kernels_list: "{{ groups['cluster_nodes']
      | map('extract', hostvars, 'ansible_kernel') }}"

  tasks:
   # Make sure the kernel list has a single unique value
   # (i.e. all kernel versions are identical)
   # We check only once for all servers in the play.
   - name: Make sure all kernel versions are aligned
     assert:
       that:
         - kernels_list | unique | count == 1
       fail_msg: "Node kernel versions are not aligned ({{ kernels_list | string }})"
     run_once: true

   - name: go on with install if assert was ok
     debug:
       msg: Go on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM