Search code examples
loopsansibleitems

Need to write loop with list of items


I have created ansible tasks to run precheck with few Linux commands like df, mount, ip a etc.. then some operations and run post check with same Linux commands. After that I will compare the pre and post check files to verify if any changes in pre and post check. Below is the list of tasks, which works fine but I would like to use ansible loops with item (item.0 for pre check and item.1 for postcheck) to reduce the complexity of the play tasks, need your support to rewrite with loops for the "Difference tasks":

    - name: Run pre-check
      shell: |
        df -Th | awk '{print $1,$2,$7}' | tail -n +2 | sort > /root/pre_post_check_{{ansible_date_time.date }}/pre_df.log
        mount > /root/pre_post_check_{{ansible_date_time.date }}/pre_mount.log
        ip a > /root/pre_post_check_{{ansible_date_time.date }}/pre_ip.log
        netstat -rn > /root/pre_post_check_{{ansible_date_time.date }}/pre_route.log
        netstat -tupln > /root/pre_post_check_{{ansible_date_time.date }}/pre_port.log
        sysctl -a > /root/pre_post_check_{{ansible_date_time.date }}/pre_sysctl.log
        uname -r > /root/pre_post_check_{{ansible_date_time.date }}/pre_uname.log

.....Operations....

    - name: Run post-check
      shell: |
        df -Th | awk '{print $1,$2,$7}' | tail -n +2 | sort > /root/pre_post_check_{{ansible_date_time.date }}/post_df.log
        mount > /root/pre_post_check_{{ansible_date_time.date }}/post_mount.log
        ip a > /root/pre_post_check_{{ansible_date_time.date }}/post_ip.log
        netstat -rn > /root/pre_post_check_{{ansible_date_time.date }}/post_route.log
        netstat -tupln > /root/pre_post_check_{{ansible_date_time.date }}/post_port.log
        sysctl -a > /root/pre_post_check_{{ansible_date_time.date }}/post_sysctl.log
        uname -r > /root/pre_post_check_{{ansible_date_time.date }}/post_uname.log

    - name: Difference
      shell: |
        echo -e "PARAMATER\tCHANGES" >> /root/report.txt
        if [[ `cat /root/pre_post_check_{{ansible_date_time.date }}/pre_df.log` == `cat /root/pre_post_check_{{ansible_date_time.date }}/post_df.log` ]]
        then
        echo -e "Mounted Filesystems - NO" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        else
        echo -e "Mounted Filesystems - YES" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        fi

        if [[ `cat /root/pre_post_check_{{ansible_date_time.date }}/pre_uname.log` == `cat /root/pre_post_check_{{ansible_date_time.date }}/post_uname.log` ]]
        then
        echo -e "KERNEL VERSION - NO" >> /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        else
        echo -e "KERNEL VERSION - YES" >> /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        fi

        if [[ `cat /root/pre_post_check_{{ansible_date_time.date }}/pre_sysctl.log` == `cat /root/pre_post_check_{{ansible_date_time.date }}/post_sysctl.log` ]]
        then
        echo -e "SYSCTL - NO" >> /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        else
        echo -e "SYSCTL - YES" >> /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        fi

Below is something I'm trying to rewrite but unable to complete it:

    - name: Difference
      shell: |
        echo -e "PARAMATER\tCHANGES" >> /root/report.txt
        if [[ `cat /root/pre_post_check_{{ansible_date_time.date }}/{{ item.prechek }}.log` == `cat /root/pre_post_check_{{ansible_date_time.date }}/{{ item.postcheck }}.log` ]]
        then
        echo -e "Mounted Filesystems - NO" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        else
        echo -e "Mounted Filesystems - YES" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        fi
      loop:
        - precheck:
          pre_df
          pre_uname
          pre_sysctl
        - postcheck:
          post_df
          post_uname
          post_sysctl

Solution

  • you could write your task like this:

    - name: Difference
      shell: |
        echo -e "PARAMATER\tCHANGES" >> /root/report.txt
        if [[ `cat /root/pre_post_check_{{ansible_date_time.date }}/pre_{{ item }}.log` == `cat /root/pre_post_check_{{ansible_date_time.date }}/post_{{ item }}.log` ]]
        then
        echo -e "Mounted Filesystems - NO" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        else
        echo -e "Mounted Filesystems - YES" > /root/pre_post_check_{{ansible_date_time.date }}/{{ inventory_hostname }}_report.log
        fi
      loop: ["df", "uname", "sysctl"]