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

标签 ansible ansible-ad-hoc

我是 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/

    相关文章:

    linux - 带有 sudo 的 Ansible adhoc 命令

    Ansible -/etc/profile.d 脚本

    ansible - 如果一场比赛失败,如何停止剧本

    ansible - 使用 ansible-galaxy 直接从 git URI 安装 Ansible 集合

    ansible - 如何格式化 Ansible 输出

    ssh - Ansible: "sudo: a password is required\r\n"

    Ansible playbook 根据操作系统版本安装不同的软件包

    Ansible:考虑使用 'become' 、 'become_method' 和 'become_user' 而不是运行 sudo

    linux - 如何以 JSON、CSV 或其他格式获取 Ansible ad-hoc 命令的输出?