需要用项目列表编写循环

Need to write loop with list of items

我已经创建了 ansible 任务来 运行 使用一些 Linux 命令进行预检查,例如 df、mount、ip a 等。然后进行一些操作和 运行 post 检查使用相同的 Linux 命令。之后我将比较预检查和 post 检查文件以验证预检查和 post 检查是否有任何变化。下面是任务列表,它工作正常,但我想对项目使用 ansible 循环(item.0 用于预检查,item.1 用于 postcheck)以减少播放任务的复杂性,需要你支持用循环重写“差异任务”:

    - name: Run pre-check
      shell: |
        df -Th | awk '{print ,,}' | 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 ,,}' | 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

下面是我正在尝试重写但无法完成的内容:

    - 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

你可以这样写你的任务:

- 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"]