为什么ansible远程复制从本地不起作用

3

我刚开始学习ansible并编写第一个copy模块,但是我不知道为什么它不起作用。

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

运行了Playbook,但没有任何反应,也没有错误提示。

# /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

我的/etc/ansible/hosts文件中有远程主机IP地址:

[osa]
10.5.1.160

详细输出:

# 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

更新

我注意到它将文件复制到本地主机,而不是远程主机,这毫无意义,为什么输出中说正在远程主机(10.5.1.160)上执行playbook?

# ls -l /tmp/bar.txt
-rw-r--r--. 1 root root 16 May 24 10:45 /tmp/bar.txt

看起来文件似乎已经存在于服务器上了!?尝试在ansible调用中添加“-v”/“-vvv” - 这看起来可行吗? - Jan Groth
为什么问题被打了负分,你认为这是一个愚蠢的问题吗?相信我,我测试了所有东西,没有任何东西在远程主机上。 - Satish
本地主机和“osa”是同一主机吗? - Kelson Silva
2
不要因为被踩而气馁。这可能意味着很多事情,包括有人心情不好——但你最好的选择是根据这些准则重新编写你的问题,这样更容易得到一个好的答案。踩通常表明你的问题缺少一些相关信息,或者以某种方式难以解析和回答,需要进行一些编辑。我将“-vv”作为标准。一个到四个v会给你越来越多的调试输出,甚至淹没你在其中。试试吧——值得的。 - Paul Hodges
具有IP地址10.5.1.161的osa,如果我在远程机器上运行一个简单的playbook来执行touch命令,它可以正常工作,但是copy命令无法正常工作,我感到很困惑。 - Satish
1个回答

3
我曾经遇到过类似的问题,它在远程服务器上寻找一个文件。你正在远程服务器上运行任务hosts: osa,但是该文件位于本地主机上。我可以通过 delegate_to: localhost 解决这个问题。"最初的回答"
---
 - hosts: osa
   tasks:
     - name: Copying file to remote host
       delegate_to: localhost
       copy: src=/tmp/foo.txt dest=/tmp/bar.txt

然而,我必须首先完成一个任务:从本地主机获取文件,然后再将其提供给远程主机。"最初的回答"
  - 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}}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接