3
votes

I just started learning ansible and writing first copy module but i am clueless why its not working.

---
 - hosts: osa
   tasks:
     - name: Copying file to remote host
       copy: src=/tmp/foo.txt dest=/tmp/bar.txt

Ran playbook but nothing happened and no error too.

# /usr/bin/ansible-playbook /home/spatel/ansible/first-playbook.yml

PLAY [osa] **************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************
ok: [10.5.1.160]

TASK [Copying file to remote host] **************************************************************************************************************************************************
ok: [10.5.1.160]

PLAY RECAP **************************************************************************************************************************************************************************
10.5.1.160                 : ok=2    changed=0    unreachable=0    failed=0

My /etc/ansible/hosts file has Remote host IP address:

[osa]
10.5.1.160

Verbose output:

# ansible-playbook -vv first-playbook.yml
ansible-playbook 2.4.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: first-playbook.yml ********************************************************************************************************************************************************
1 plays in first-playbook.yml

PLAY [osa] **************************************************************************************************************************************************************************
META: ran handlers

TASK [Copying file to remote host] **************************************************************************************************************************************************
task path: /home/spatel/ansible/first-playbook.yml:5
ok: [10.5.1.160] => {"changed": false, "checksum": "4383f040dc0303e55260bca327cc0eeb213b04b5", "failed": false, "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/tmp/bar.txt", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 8, "state": "file", "uid": 0}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************************************************************
10.5.1.160                 : ok=1    changed=0    unreachable=0    failed=0

UPDATE

I have noticed its copying file to localhost not remote host, this makes no sense, why is it saying in the output that it is executing the playbook on the remote host (10.5.1.160) ?????

# ls -l /tmp/bar.txt
-rw-r--r--. 1 root root 16 May 24 10:45 /tmp/bar.txt
1
It looks as if the file would already exist on the server!? Try adding -v / -vvv to the ansible call - does this all look plausible?jan groth
why negative mark on question, do you think this is a silly question? Believe me i test everything nothing is on remote host.Satish
Is localhost and 'osa' the same host?Kelson Silva
Don't be discouraged by a downvote. It could mean any of a lot of things, including someone just being pissy -- but your best bet is to rewrite your question according to these guidelines, which will more likely get a good answer sooner. Downvotes are usually an indication that your question is lacking some relevant info, or is otherwise somehow difficult to parse and answer without some editing. I use -vv as a standard. one to four v's will give you increasing amounts of debugging output, to the point of drowning you in it. Try it - it's worth it.Paul Hodges
osa with 10.5.1.161 IP address, If i run simple playbook doing touch is remote machine /bin/touch it works but copy doesn't i am clue less.Satish

1 Answers

2
votes

I had a similar problem that it was looking for a file on the remote server. You are running your tasks on remote server hosts: osa but the file is on localhost. I could solve it with delegate_to: localhost.

---
 - hosts: osa
   tasks:
     - name: Copying file to remote host
       delegate_to: localhost
       copy: src=/tmp/foo.txt dest=/tmp/bar.txt

However, I had to make even first a task to get the file from localhost and then to provide it afterwards to the remote host:

  - name: read files

    # This needs to run on localhost, because that's where
    # the keys are stored.
    delegate_to: localhost

    command: cat {{item}}

    # Register the results of this task in a variable called
    # "files"
    register: files

    with_fileglob:
      - "/tmp/*.txt"

  - name: show what was stored in the files variable
    debug:
      var: files

  - name: Copying file to remote host
     delegate_to: localhost
     copy: src="{{item.stdout}}" dest=/tmp/bar.txt
    with_items: "{{keys.results}}"