【问题标题】:check mount point usage using ansible使用 ansible 检查挂载点使用情况
【发布时间】:2017-02-15 19:31:09
【问题描述】:

我正在尝试使用 ansible 列出使用率超过 50% 的挂载点。我在 AWK 上遇到错误。

如果我单独运行这个命令,它会起作用

df -P | awk '$5 >=90 {print}'
Filesystem     1024-blocks     Used Available Capacity Mounted on
tmpfs              2097152  1948868    148284      93% /tmp

df -P | grep /tmp | awk '$5 >=90 {print}'
tmpfs              2097152  1948832    148320      93% /tmp

如果我将相同的命令放在 ansible shell 中,它会失败

这里:

ansible all -i <hostname>, -m shell -a "df -P | grep /tmp | awk '$5 >=90 {print}'"
SSH password:
SUDO password[defaults to SSH password]:
<hostname> | FAILED | rc=1 >>
awk:  >=90 {print}
awk:  ^ syntax error
grep: write error: Broken pipe

有没有办法做到这一点?有一个更好的方法吗?可能是使用因素?

【问题讨论】:

  • $5 被您当前的 shell(从您运行 ansible 的地方)替换,转义它。

标签: linux shell ansible ansible-facts


【解决方案1】:

感谢康斯坦丁·苏沃洛夫,您的回答完美。

ansible all -i <hostname>, -m shell -a "df -P | grep /tmp | awk '\$5 >=50 {print}'"
SSH password:
SUDO password[defaults to SSH password]:
<hostname> | SUCCESS | rc=0 >>
/dev/sda8           944808      55484     840496       7% /var/tmp
tmpfs              2097152    1858344     238808      89% /tmp

【讨论】:

    【解决方案2】:

    @user3330284:你可以去掉 grep 的使用并尝试关注(虽然没有测试过):

    ansible all -i <hostname>, -m shell -a "df -P  | awk '/\/tmp/{if(\$5 >=50){print}}'"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2020-02-19
      • 2014-04-05
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多