需要用项目列表编写循环

发布于 2025-02-03 01:49:35 字数 3917 浏览 3 评论 0原文

我创建了Ansible任务,以使用少数Linux命令(例如DF,Mount,Ip a等)运行Precheck。然后使用相同的Linux命令运行一些操作并运行后检查。之后,我将比较预先检查文件,以验证检查前检查中是否有任何更改。以下是任务列表,它可以正常工作,但我想使用带有项目的Ansible循环(it.0.0 for pre preck and item.1 for postcheck)来降低播放任务的复杂性,需要您的支持来通过循环重写对于“差异任务”:

    - 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

下面是我试图重写但无法完成的事情:

    - 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2025-02-10 01:49:35

您可以这样写任务:

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

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"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文