非root用户使用错误的目标路径获取模块

时间:2016-07-26 19:52:14

标签: ansible ansible-playbook

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

Ansible位于:10.1.31.81 Logserver位于:10.1.31.82

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

Logserver文件路径:/ export / home / dscadmin / temp / stuff /

此文件夹中的文件: 1.登录 2.登录 3.log 4.日志 5.log

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

我怀疑fetch模块中的一些问题。如果有人遇到同样的问题,请告诉我。

问题是fetch将dest设置为:“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

1 个答案:

答案 0 :(得分:0)

fetch模块没有问题 become_user仅用于远程主机,本地用户仍然是您运行的用户 您使用fetch致电dest={{v_log_folder}}/模块 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