Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a string to file using Ad-Hoc Commands in Ansible

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!

like image 609
firebolt Avatar asked Dec 22 '16 21:12

firebolt


People also ask

How do I write an ad hoc in ansible?

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.

What format does ansible ad hoc command returns the output?

Use the -o option to display the output of Ansible ad hoc commands in a single line format.


1 Answers

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.

like image 171
techraf Avatar answered Nov 13 '22 06:11

techraf