Home > Blockchain >  Is there any alternative to stdout.line in ansible?
Is there any alternative to stdout.line in ansible?

Time:03-23

I am facing an issue with stdout.lines in ansible. Are there any alternatives available? This is my code

- hosts: myhost
  tasks:
    - name: get contents 
      shell: "ls /home/*.iso | cut -d '/' -f 7 "
      register: prod

    - name: Copy
      community.vmware.vsphere_copy:
        hostname: host
        username: myuser
        password: mypass
        src: "{{ my_path }}"
        datacenter: VMFStorage
        datastore: SAN-0
        path: "/test_path/{{ prod.stdout_lines }}"
        validate_certs: false

When I run this, the output I'm getting in UI is

['test.iso']

But the expected output is

test.iso

Any help would be appreciated.

[EDIT] Tried using the find command

- hosts: myhost
  tasks:
    - name: find the file
      find:
        paths: /home/
        patterns: '*.iso'
      register: prod
    - debug: var=item.path
      with_items: "{{ prod.files }}"

    - name: Copy
      community.vmware.vsphere_copy:
        hostname: host
        username: myuser
        password: mypass
        src: "{{ my_path }}"
        datacenter: VMFStorage
        datastore: SAN-0
        path: "/test_path/{{ prod.files }}"
        validate_certs: false

Also, in the "path" defined under community.vmware.vsphere_copy module, I just require the name test.iso instead the whole path /home/test.iso

Output of debug:

 [myhost] => (item={'path': '/home/linux.iso', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 0, 'gid': 0, 'size': 1295247360, 'inode': 134527790, 'dev': 64776, 'nlink': 1, 'atime': 1648020499.1899607, 'mtime': 1648013066.2809734, 'ctime': 1648020450.6790545, 'gr_name': 'root', 'pw_name': 'root', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}) => {
    "ansible_loop_var": "item",
    "item": {
        "atime": 1648020499.1899607,
        "ctime": 1648020450.6790545,
        "dev": 64776,
        "gid": 0,
        "gr_name": "root",
        "inode": 134527790,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0644",
        "mtime": 1648013066.2809734,
        "nlink": 1,
        "path": "/home/linux.iso",
        "pw_name": "root",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 1295247360,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    },
    "item.path": "/home/linux.iso"
}

CodePudding user response:

loop over prod.files, take the path key and use the basename filter:

- name: Copy
  community.vmware.vsphere_copy:
    hostname: host
    username: myuser
    password: mypass
    src: "{{ my_path }}"
    datacenter: VMFStorage
    datastore: SAN-0
    path: "/test_path/{{ item.path | basename }}"
    validate_certs: false
  loop: "{{ prod.files }}"

see filters

  • Related