需要用项目列表编写循环
我创建了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样写任务:
you could write your task like this: