Deepak Sharma
Deepak Sharma

Reputation: 23

Copy a file to host machine in Ansible

I have to perform the below steps.

     Create a Playbook test.yml.
     This playbook should copy the file (somefile.j2) to the Host machine's folder1, only if 
     somefile.j2 does not exist in host01.
     By using vi editor you can add the tasks to test.yml.
     [Hint: Use the stat and template module].

somefile.j2 is present at /root.

An inventory file named "myhosts" is present at /root

    $ cat myhosts
    host01 ansible_ssh_user=root

What should be the contents of test.yml?

Upvotes: 0

Views: 64

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68254

Homework questions without "the work done so far to solve the problem and a description of the difficulty" is off-topic. But the conflict in the assignment, which might be considered "a practical, answerable problem that is unique to software development", deserves the answer.

  • Copy the file to the host only if somefile.j2 does not exist
  • Use the stat and template module

The assignment requires to use the module stat to find out whether the file exists or not. If it does not exist use the module template to create it.

It is not necessary to use the module stat. The module template "will only transfer the file if the destination does not exist" when "force: no" (default yes). Such "idempotent" behavior of Ansible modules is essential, should be expected, and searched for.


Simply take a look at the examples to see "What should be the contents of test.yml?"

Upvotes: 1

Related Questions