ansible rsync或将随机命名的文件复制到远程计算机

ansible rsync或将随机命名的文件复制到远程计算机,ansible,ansible-playbook,ansible-2.x,Ansible,Ansible Playbook,Ansible 2.x,我正在使用ansible 2.1进行rsync或将文件从主机复制到远程主机。该文件位于目录中,但名称中有一个随机字符串。我尝试使用ls-d通过shell命令获取名称,并尝试注册此值,但显然,我使用的语法导致角色失败。有没有想过我可能做错了什么 --- - name: copying file to server - local_action: shell cd /tmp/directory/my-server/target/ - local_action: shell ls -d myfile

我正在使用ansible 2.1进行rsync或将文件从主机复制到远程主机。该文件位于目录中,但名称中有一个随机字符串。我尝试使用ls-d通过shell命令获取名称,并尝试注册此值,但显然,我使用的语法导致角色失败。有没有想过我可能做错了什么

---
- name: copying file to server 
- local_action: shell cd /tmp/directory/my-server/target/
- local_action: shell ls -d myfile*.jar
  register: test_build
- debug: msg={{ test_build.stdout }}
- copy: src=/tmp/directory/my-server/target/{{ test_build.stdout }}  dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
  become: true
  become_user: ubuntu 
  become_method: sudo
例外情况

fatal: [testserver]: FAILED! => {"failed": true, "reason": "no action detected in task. This often indicates a misspelled module name, or incorrect module path.\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/move.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring file to server\n  ^ here\n\n\nThe error appears to have been in '/home/user/test/roles/test-server/tasks/synchronize.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: transferring artifact to server\n  ^ here\n"}

您需要简化您的命令。您也不希望在模块名称前加连字符。这将导致语法错误,因为它无法将该模块标识为操作。每个任务只能调用一个模块。例如,这是行不通的

- name: task one
  copy: src=somefile dest=somefolder/
  copy: src=somefile2 dest=somefolder2/
这两项任务需要分为两个独立的任务。你的剧本也一样。请执行以下操作:

  - name: copying file to server 
    local_action: "shell ls -d /tmp/directory/my-server/target/myfile*.jar"
    register: test_build
  - debug: msg={{ test_build.stdout }}

  - name: copy the file
    copy: src={{ test_build.stdout }}  dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
如果可能的话,在剧本中插入“变成”,而不是在tasks/main.yml文件中,除非您只想将“变成”用于这两个任务,并且稍后将向同一剧本中添加更多任务


注意:调试消息行是完全可选的。它不会以任何方式影响playbook的结果,它只会向您显示通过shell“ls”命令找到的文件夹/文件名。

您需要简化命令。您也不希望在模块名称前加连字符。这将导致语法错误,因为它无法将该模块标识为操作。每个任务只能调用一个模块。例如,这是行不通的

- name: task one
  copy: src=somefile dest=somefolder/
  copy: src=somefile2 dest=somefolder2/
这两项任务需要分为两个独立的任务。你的剧本也一样。请执行以下操作:

  - name: copying file to server 
    local_action: "shell ls -d /tmp/directory/my-server/target/myfile*.jar"
    register: test_build
  - debug: msg={{ test_build.stdout }}

  - name: copy the file
    copy: src={{ test_build.stdout }}  dest=/home/ubuntu/ owner=ubuntu group=ubuntu mode=644 backup=yes
如果可能的话,在剧本中插入“变成”,而不是在tasks/main.yml文件中,除非您只想将“变成”用于这两个任务,并且稍后将向同一剧本中添加更多任务


注意:调试消息行是完全可选的。它不会以任何方式影响playbook的结果,它只会向您显示通过shell“ls”命令找到的文件夹/文件名。

您应该避免使用
shell
命令,如果可能的话,这是一种反模式。
在您的情况下,您可以使用
fileglob
如下所示:

- name: Copy file 
  copy:
    src: "{{ lookup('fileglob','/tmp/directory/my-server/target/myfile*.jar', wantlist=true) | first }}"
    dest: /home/ubuntu/
    owner: ubuntu
    group: ubuntu
    mode: 644
    backup: yes

如果你100%确定只有一个这样的文件,你可以省略
wantlist=true
|first
–我用它作为一个安全网,只过滤第一个条目,如果有很多的话。

你应该避免
shell
命令,如果可能的话,这是一种可接受的反模式。
在您的情况下,您可以使用
fileglob
如下所示:

- name: Copy file 
  copy:
    src: "{{ lookup('fileglob','/tmp/directory/my-server/target/myfile*.jar', wantlist=true) | first }}"
    dest: /home/ubuntu/
    owner: ubuntu
    group: ubuntu
    mode: 644
    backup: yes

如果你100%确定只有一个这样的文件,你可以省略
wantlist=true
|first
——我用它作为一个安全网,只过滤第一个条目,如果有很多的话。

谢谢@avalon-会尝试回来的。你太棒了!谢谢@avalon-我会努力回来的。你太棒了!spaciba@Konstantin-非常有用查找适用于我的一个案例,其中我需要在本地运行它-但我认为如果它需要在远程机器上运行它将不起作用-有没有关于在远程机器上执行此操作的正确方法的想法(否则我将使用带ls-d的shell)您在问题中使用
local\u action
,因此,
查找
是首选方法。如果你想在远程机器上使用它,您应该使用
find
模块并注册搜索结果。spaciba Konstantinspaciba@Konstantin-非常有用查找适用于我的一个案例,其中我需要在本地运行此功能-但我认为如果需要在远程机器上运行,它将不起作用-您有没有想过在远程机器上执行此操作的正确方法(否则我将使用ls-d的shell)您在问题中使用
local\u action
,因此
查找
是首选方法。如果您希望在远程机器上使用此方法,您应该使用
查找
模块并注册搜索结果。spaciba Konstantin