ok: [localhost] => (item=sda) => { "changed": false, "disk": { "dev": "/dev/sda", "logical_block": 512, "model": "VMware Virtual disk", "physical_block": 512, "size": 33554432.0, "table": "msdos", "unit": "kib" }, "invocation": { "module_args": { "align": "optimal", "device": "/dev/sda", "flags": null, "label": null, "name": null, "number": null, "part_end": "100%", "part_start": "0%", "part_type": "primary", "state": "info", "unit": "KiB" } }, "item": "sda", "partitions": [ { "begin": 1024.0, "end": 308224.0, "flags": [ "boot" ], "fstype": "ntfs", "num": 1, "size": 307200.0, "unit": "kib" }, { "begin": 308224.0, "end": 33553408.0, "flags": [], "fstype": "ntfs", "num": 2, "size": 33245184.0, "unit": "kib" } ], "script": "unit 'KiB' print"}---## This playbook will backup user data from HD's found
- name: Backup data hosts: all connection: local become: true become_user: root become_method: su #vars_files: # - vault.yml #remote_user: root
tasks: - name: Install jmespath via pip if missing pip: name: jmespath - name: Check for hard drive devices setup: filter: ansible_devices
- name: Check for hard drive devices parted: device=/dev/{{ item }} register: driveFStype with_items: "{{ json_query('*.partitions.keys(@)[]') }}"
- name: mount: path: "/mnt/{{ item }}" src: "/dev/{{ item }}" fstype: ntfs state: mounted with_items: "{{ ansible_devices | json_query('*.partitions.keys(@)[]') }}"
---## This playbook will backup user data from HD's found
- name: Backup data hosts: all connection: local become: true become_user: root become_method: su #vars_files: # - vault.yml #remote_user: root
tasks: - name: Install jmespath via pip if missing pip: name: jmespath - name: Check for hard drive fstype
parted: device=/dev/{{ item }} register: whichFS with_items: "{{ json_query('*.partitions.fstype.keys(@)[]') }}"
- name: Check for hard drive devices parted: device=/dev/{{ item }} register: whichPartition with_items: "{{ json_query('*.partitions.keys(@)[]') }}"
- name: mount: path: "/mnt/{{ item }}" src: "/dev/{{ item }}" fstype: ntfs state: mounted with_items: "{{ ansible_devices | json_query('*.partitions.keys(@)[]') }}"
when: "{{ whichFS }}" == ntfs
tasks: - name: Install jmespath via pip if missing pip: name: jmespath
- name: check for hard drive devices setup: filter: ansible_devices tags: - test
- name: get device info parted: device=/dev/{{ item.key }} unit=MiB register: results tags: - test ignore_errors: yes with_dict: "{{ ansible_devices }}"
- name: mount ntfs partitions mount: path: "/mnt/{{ item.item }}" src: "{{ item.disk.dev }}" fstype: ntfs when: "item.partitions | length > 0 and item.partitions | selectattr('fstype', 'equalto', 'ntfs') | list | length > 0" with_items: "{{ results.results }}" tags: - testPLAY [Backup customer data] **************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************
ok: [localhost]
TASK [check for hard drive devices] ******************************************************************************************************************************************************************
ok: [localhost]
TASK [get device info] *******************************************************************************************************************************************************************************
failed: [localhost] (item={'key': u'sr0', 'value': {u'scheduler_mode': u'cfq', u'sectorsize': u'512', u'vendor': u'NECVMWar', u'sectors': u'2097151', u'sas_device_handle': None, u'sas_address': None, u'host': u'IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)', u'rotational': u'1', u'removable': u'1', u'support_discard': u'0', u'holders': [], u'partitions': {}, u'model': u'VMware IDE CDR10', u'size': u'1024.00 MB'}}) => {"err": "Error: Error opening /dev/sr0: No medium found\n", "failed": true, "item": {"key": "sr0", "value": {"holders": [], "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)", "model": "VMware IDE CDR10", "partitions": {}, "removable": "1", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "cfq", "sectors": "2097151", "sectorsize": "512", "size": "1024.00 MB", "support_discard": "0", "vendor": "NECVMWar"}}, "msg": "Error while getting device information with parted script: '/usr/sbin/parted -s -m /dev/sr0 -- unit 'MiB' print'", "out": "", "rc": 1}
ok: [localhost] => (item={'key': u'sda', 'value': {u'scheduler_mode': u'cfq', u'sectorsize': u'512', u'vendor': u'VMware', u'sectors': u'67108864', u'sas_device_handle': None, u'sas_address': None, u'host': u'Serial Attached SCSI controller: LSI Logic / Symbios Logic SAS1068 PCI-X Fusion-MPT SAS (rev 01)', u'rotational': u'1', u'removable': u'0', u'support_discard': u'0', u'holders': [], u'partitions': {u'sda2': {u'sectorsize': 512, u'uuid': u'A880A12480A0FA48', u'sectors': u'66490368', u'start': u'616448', u'holders': [], u'size': u'31.71 GB'}, u'sda1': {u'sectorsize': 512, u'uuid': u'9844A05B44A03E3E', u'sectors': u'614400', u'start': u'2048', u'holders': [], u'size': u'300.00 MB'}}, u'model': u'Virtual disk', u'size': u'32.00 GB'}})
...ignoring
TASK [mount ntfs partitions] *************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'item.partitions | length > 0 and item.partitions | selectattr('fstype', 'equalto', 'ntfs') | list | length > 0' failed. The error was: error while evaluating conditional (item.partitions | length > 0 and item.partitions | selectattr('fstype', 'equalto', 'ntfs') | list | length > 0): 'dict object' has no attribute 'partitions'\n\nThe error appears to have been in '/home/svcacctansible/automation/backup/customerBackup.yml': line 35, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: mount ntfs partitions\n ^ here\n"}
to retry, use: --limit @/home/svcacctansible/automation/backup/customerBackup.retry
PLAY RECAP *******************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=1
tasks: - name: Install jmespath via pip if missing pip: name: jmespath
- name: run the setup module for some facts about the hardware setup: filter: ansible_devices tags: - new
- name: check what drives are available parted: device: /dev/{{ item }} with_items: "{{ ansible_devices }}" when: - item != 'sr0' register: result
- name: get a list of paritions that are only ntfs debug: msg: "{{ item.0.disk.dev }}{{ (item.0.disk.table == 'loop') | ternary('', item.1.num) }}" when: item.1.fstype == 'ntfs' with_subelements: - "{{ result.results }}" - partitions
- name: mount ntfs partitions mount: path: "/mnt/{{ item.0.disk.dev }}{{ (item.0.disk.table == 'loop') | ternary('', item.1.num) }}" src: "{{ item.0.disk.dev }}{{ (item.0.disk.table == 'loop') | ternary('', item.1.num) }}" fstype: ntfs state: mounted when: item.1.fstype == 'ntfs' # when: results.item.value.partitions.fstype == "ntfs" with_subelements: - "{{ result.results }}" - partitions