我是 Ansible 的初学者,正在尝试使用 将字符串写入文件。临时命令 我正在尝试使用 replace
模块。我要写入的文件是 /etc/motd/
.
ansible replace --sudo /etc/motd "This server is managed by Ansible"
任何帮助将不胜感激谢谢!
最佳答案
看看 lineinfile
module Ad hoc commands 的用法和一般语法.
您正在寻找的是:
ansible target_node -b -m lineinfile -a 'dest=/etc/motd line="This server is managed by Ansible"'
扩展形式:
ansible target_node --become --module-name=lineinfile --args='dest=/etc/motd line="This server is managed by Ansible"'
解释:
target_node
是在 Ansible inventory file 中定义的主机名或组名--become
( -b
) 指示 Ansible 使用 sudo
-module-name
( -m
) 指定要运行的模块 (lineinfile
这里) --args
( -a
) 将参数传递给模块(这些变化取决于模块)dest
指向目标文件line
指示 Ansible 确保文件中的特定行 如果您想替换
/etc/motd
的全部内容你应该使用 copy
module .ansible target_node -b -m copy -a 'dest=/etc/motd content="This server is managed by Ansible"'
请注意,其中一个参数已相应更改。
关于ansible - 在 Ansible 中使用 Ad-Hoc 命令将字符串写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291899/