使用不正确的目标路径为非 root 用户获取模块

fetch module for non root user using incorrect destination path

我有 2 个用户帐户 1根 2-dscadmin(这是非根用户)

Ansible 在:10.1.31.81 日志服务器位于:10.1.31.82

我想将日志文件从logserver dscadmin 用户复制到ansible 的dscadmin。

日志服务器文件路径:/export/home/dscadmin/temp/stuff/

此文件夹中的文件: 1.log 2.log 3.log 4.log 5.log

现在的问题是,在使用 fetch 命令时,它会将获取的文件存储在 Ansible 服务器的 /root/temp/stuff/ 而不是 /export/home/dscadmin/temp/stuff/ 中,因为 ssh 连接是通过 root 用户建立的。成为:和 become_user:在这方面有帮助。

我怀疑抓取中的某些问题module.please如果有人有同样的问题请告诉我。

问题是 fetch set the dest as : "dest": "/root/temp/stuff/3.log",

剧本:

---
- hosts: logserver     
  become: yes
  become_user: dscadmin
  vars:
  v_log_folder: ~/temp/stuff

  tasks:
  - name: List generated log files
    shell: ls -1 {{v_log_folder}}/*.log
    register: reg_generatefiles

#    - debug: var=reg_generatefiles.stdout_lines

  - name: Fetch log files
    fetch: src={{ item }}  dest={{v_log_folder}}/ flat=yes
    with_items: '{{reg_generatefiles.stdout_lines}}' 
    register: reg_fetchfile

  - debug: var=reg_fetchfile

日志:

    [root@comiskey-v01 ansible]# ansible-playbook 2.yml 

    PLAY [logserver] ***************************************************************

    TASK [setup] *******************************************************************
    ok: [10.1.31.82]

    TASK [List generated log files] ************************************************
    changed: [10.1.31.82]

    TASK [Fetch log files] *********************************************************
    ok: [10.1.31.82] => (item=/export/home/dscadmin/temp/stuff/1.log)
    ok: [10.1.31.82] => (item=/export/home/dscadmin/temp/stuff/2.log)
    ok: [10.1.31.82] => (item=/export/home/dscadmin/temp/stuff/3.log)
    ok: [10.1.31.82] => (item=/export/home/dscadmin/temp/stuff/4.log)
    ok: [10.1.31.82] => (item=/export/home/dscadmin/temp/stuff/5.log)

    TASK [debug] *******************************************************************
    ok: [10.1.31.82] => {
        "reg_fetchfile": {
            "changed": false, 
            "msg": "All items completed", 
            "results": [
                {
                    "_ansible_item_result": true, 
                    "_ansible_no_log": false, 
                    "changed": false, 
                    "checksum": "716b39d3876683a48ca96e00f104f94187679920", 
                    "dest": "/root/temp/stuff/1.log", 
                    "file": "/export/home/dscadmin/temp/stuff/1.log", 
                    "invocation": {
                        "module_args": {
                            "dest": "~/temp/stuff/", 
                            "flat": "yes", 
                            "src": "/export/home/dscadmin/temp/stuff/1.log"
                        }, 
                        "module_name": "fetch"
                    }, 
                    "item": "/export/home/dscadmin/temp/stuff/1.log", 
                    "md5sum": "68e1d0d084c7e7387bfde847d0b14e2e"
                }, 
                {
                    "_ansible_item_result": true, 
                    "_ansible_no_log": false, 
                    "changed": false, 
                    "checksum": "716b39d3876683a48ca96e00f104f94187679920", 
                    "dest": "/root/temp/stuff/2.log", 
                    "file": "/export/home/dscadmin/temp/stuff/2.log", 
                    "invocation": {
                        "module_args": {
                            "dest": "~/temp/stuff/", 
                            "flat": "yes", 
                            "src": "/export/home/dscadmin/temp/stuff/2.log"
                        }, 
                        "module_name": "fetch"
                    }, 
                    "item": "/export/home/dscadmin/temp/stuff/2.log", 
                    "md5sum": "68e1d0d084c7e7387bfde847d0b14e2e"
                }, 
                {
                    "_ansible_item_result": true, 
                    "_ansible_no_log": false, 
                    "changed": false, 
                    "checksum": "716b39d3876683a48ca96e00f104f94187679920", 
                    "dest": "/root/temp/stuff/3.log", 
                    "file": "/export/home/dscadmin/temp/stuff/3.log", 
                    "invocation": {
                        "module_args": {
                            "dest": "~/temp/stuff/", 
                            "flat": "yes", 
                            "src": "/export/home/dscadmin/temp/stuff/3.log"
                        }, 
                        "module_name": "fetch"
                    }, 
                    "item": "/export/home/dscadmin/temp/stuff/3.log", 
                    "md5sum": "68e1d0d084c7e7387bfde847d0b14e2e"
                }, 
                {
                    "_ansible_item_result": true, 
                    "_ansible_no_log": false, 
                    "changed": false, 
                    "checksum": "716b39d3876683a48ca96e00f104f94187679920", 
                    "dest": "/root/temp/stuff/4.log", 
                    "file": "/export/home/dscadmin/temp/stuff/4.log", 
                    "invocation": {
                        "module_args": {
                            "dest": "~/temp/stuff/", 
                            "flat": "yes", 
                            "src": "/export/home/dscadmin/temp/stuff/4.log"
                        }, 
                        "module_name": "fetch"
                    }, 
                    "item": "/export/home/dscadmin/temp/stuff/4.log", 
                    "md5sum": "68e1d0d084c7e7387bfde847d0b14e2e"
                }, 
                {
                    "_ansible_item_result": true, 
                    "_ansible_no_log": false, 
                    "changed": false, 
                    "checksum": "716b39d3876683a48ca96e00f104f94187679920", 
                    "dest": "/root/temp/stuff/5.log", 
                    "file": "/export/home/dscadmin/temp/stuff/5.log", 
                    "invocation": {
                        "module_args": {
                            "dest": "~/temp/stuff/", 
                            "flat": "yes", 
                            "src": "/export/home/dscadmin/temp/stuff/5.log"
                        }, 
                        "module_name": "fetch"
                    }, 
                    "item": "/export/home/dscadmin/temp/stuff/5.log", 
                    "md5sum": "68e1d0d084c7e7387bfde847d0b14e2e"
                }
            ]
        }
    }

    PLAY RECAP *********************************************************************
    10.1.31.82                 : ok=4    changed=1    unreachable=0    failed=0

fetch 模块没有问题。
become_user 仅用于远程主机,本地用户仍然是您 运行 ansible 使用的用户。
您使用 dest={{v_log_folder}}/.
调用 fetch 模块 v_log_folder 设置为 ~/temp/stuff 并且您 运行 可以与 root 用户一起使用。
所以真实路径被正确扩展为:/root/temp/stuff/.

如果你 运行 在本地 root 用户下使用 ansible,你可以将文件放在任何你想要的地方。
要将文件提取到与目标服务器相同的路径,您可以使用:

- name: Fetch log files
  fetch: src={{ item }}  dest={{ item }} flat=yes
  with_items: '{{ reg_generatefiles.stdout_lines }}' 
  register: reg_fetchfile