ansible 替换rm命令,减少危险删除的风险

众所周知rm命令的杀伤力,所以执行rm -rf要格外小心。为了避免意外删除文件,最佳实践是在“.bashrc”文件中创建’rm’命令别名为’rm -i’,它会要求确认每次删除。
这里就不列举出可能的误删除命令,这个脚本主要是用于避免删除根目录,导致系统直接异常。

vim ~/.bashrc

safe_rm() {
    option=""
    for file in "$@"; do
        if [[ $file =~ ^- ]]; then
            option="$option $file"
            continue
        fi
        p=$(realpath "$file")
        if [[ $p =~ ^/[^/]*$ ]]; then
            echo "Try to remove $p. Dangerous!!!"
        else
            #echo "hahhaaa" # 在测试的时候,可以将这一行改为echo
            command rm -i $option "$file" 
        fi
    done
}
alias rm='safe_rm'

source ~/.bashrc

ansible配置

- name: Update .bashrc and source it
  hosts: parserver_test
  become: yes

  tasks:
    - name: Add content to .bashrc
      lineinfile:
        path: /etc/bashrc
        line: |

          safe_rm() {
              option=""
              for file in "$@"; do
                  if [[ $file =~ ^- ]]; then
                      option="$option $file"
                      continue
                  fi
                  p=$(realpath "$file")
                  if [[ $p =~ ^/[^/]*$ ]]; then
                      echo "Try to remove $p. Dangerous!!!"
                  else
                      #echo "hahhaaa" # 在测试的时候,可以将这一行改为echo
                      command rm -i $option "$file"
                  fi
              done
          }
          alias rm='safe_rm'
        insertafter: EOF




    - name: Source .bashrc and run a command
      #command: "source ~/.bashrc && your_command_here"
      shell: source /etc/bashrc

其中 source /etc/bashrc 和 path: /etc/bashrc
可以按照实际情况改为source ~/.bashrc

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值