使用 shell 模块时,有时为了偷懒 lineinfile,用 sed 替换,涉及到使用 sed 自己变量时,会报错
例:将最后一行注释掉
ansible all -i 172.28.128.3, -m shell -a "sed -e '$s/^/#/g' /etc/hosts" -u vagrant -s 172.28.128.3 | FAILED | rc=1 >> sed: -e expression #1, char 4: comments don't accept any addresses
由于 $s 代表最后一行,需要转义一下
ansible all -i 172.28.128.3, -m shell -a "sed -i '\$s/#//g' /etc/hosts" -u vagrant -s
Leave a Reply