在 Ansible 中使用 Ad-Hoc 命令将字符串写入文件

fir*_*olt 6 ansible ansible-ad-hoc

我是 Ansible 的初学者,并尝试使用Ad-Hoc 命令将字符串写入文件,我正在尝试使用该replace模块。我要写入的文件是/etc/motd/.

ansible replace --sudo /etc/motd "This server is managed by Ansible"
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激谢谢!

tec*_*raf 6

查看lineinfile模块用法和Ad hoc 命令的一般语法。

您正在寻找的是:

ansible target_node -b -m lineinfile -a 'dest=/etc/motd line="This server is managed by Ansible"'
Run Code Online (Sandbox Code Playgroud)

扩展形式:

ansible target_node --become --module-name=lineinfile --args='dest=/etc/motd line="This server is managed by Ansible"'
Run Code Online (Sandbox Code Playgroud)

解释:

  • target_node是 Ansible清单文件中定义的主机名或组名

  • --become( -b) 指示 Ansible 使用sudo

  • -module-name( -m) 指定要运行的模块(lineinfile此处)

  • --args( -a) 将参数传递给模块(这些变化取决于模块)

    • dest 指向目标文件
    • line 指示 Ansible 确保特定行在文件中

如果您想替换整个内容,/etc/motd您应该使用copymodule

ansible target_node -b -m copy -a 'dest=/etc/motd content="This server is managed by Ansible"'
Run Code Online (Sandbox Code Playgroud)

请注意其中一个参数已相应更改。