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!
An Ansible ad hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. ad hoc commands are quick and easy, but they are not reusable.
Use the -o option to display the output of Ansible ad hoc commands in a single line format.
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 fileline
instructs Ansible to ensure a particular line is in the fileIf 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With