0
votes

i have 2 user account 1-root 2-dscadmin (this is non-root user)

Ansible is in : 10.1.31.81 Logserver is in : 10.1.31.82

I want to copy log files from logserver dscadmin user to dscadmin of ansible.

Logserver file path: /export/home/dscadmin/temp/stuff/

Files in this folder: 1.log 2.log 3.log 4.log 5.log

Now problem is while using the fetch command it stored the fetched file in /root/temp/stuff/ instead of /export/home/dscadmin/temp/stuff/ of Ansible server as ssh connection is established over root user. become: and become_user: does help in this.

i doubt some issue in the fetch module.please let me know on this if anyone having same issue.

issue is fetch set the dest as : "dest": "/root/temp/stuff/3.log",

Playbook:

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

Logs:

    [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

1 Answers

1
votes

There is no issue with fetch module.
become_user is used for remote host only, local user is still the one you run ansible with.
You call fetch module with dest={{v_log_folder}}/.
v_log_folder is set to ~/temp/stuff and you run ansible with root user.
So the real path is correctly expanded to: /root/temp/stuff/.

If you run ansible under root user locally, you can place files anywhere you want.
To fetch files into the same path as on target server, you can use:

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