8

I'm a beginner with Ansible and trying to write a string to a file with an Ad-Hoc command I'm trying to play around with the replace module. The file I'm trying to write to is /etc/motd/.

ansible replace --sudo /etc/motd "This server is managed by Ansible"

Any help would be appreciated thanks!

techraf
  • 64,883
  • 27
  • 193
  • 198
firebolt
  • 139
  • 3
  • 7

1 Answers1

8

Have a look at the lineinfile module usage and a general syntax for Ad hoc commands.

What you are looking for is:

ansible target_node -b -m lineinfile -a 'dest=/etc/motd line="This server is managed by Ansible"'

in extended form:

ansible target_node --become --module-name=lineinfile --args='dest=/etc/motd line="This server is managed by Ansible"'

Explanation:

  • target_node is the hostname or group name as defined in the Ansible inventory file

  • --become (-b) instructs Ansible to use sudo

  • -module-name (-m) specifies the module to run (lineinfile here)

  • --args (-a) passes arguments to the module (these change depending on a module)

    • dest points to the destination file
    • line instructs Ansible to ensure a particular line is in the file

If you would like to replace the whole contents of the /etc/motd you should use copy module.

ansible target_node -b -m copy -a 'dest=/etc/motd content="This server is managed by Ansible"'

Notice one of the arguments is changed accordingly.

techraf
  • 64,883
  • 27
  • 193
  • 198