I was able to resolve this by redoing all of my tasks as follows:

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



On Tuesday, July 25, 2017 at 8:42:25 AM UTC-4, Gilberto Valentin wrote:
>
> Hello,
>
> I have a playbook that scans for hard drives and mounts them in order to 
> do a backup of the data from those drives. I am able to scan for the drives 
> just fine and I can also mount them (thanks to the Ansible IRC community). 
> The only issue I am having is mounting ONLY when the partition is *fstype: 
> ntfs*. As you can see below, when I run the *parted* module, I can see a 
> key value of *fstype: ntfs*. 
>
> 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"
> }
>
> I use the setup module to scan for the devices and the parted module to 
> see the fstype. Thinking about it further, I should probably just use the 
> parted module to get me both since parted does provide both.
>
> Using *parted* alone without *setup*, how can I tell my mount task to 
> just mount ONLY if the fstype is ntfs? Here is my current playbook:
>
> ---
> ## 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(@)[]') }}"
>
>
>
> The *Check for hard drive device *task above doesn't work. The rest of 
> the tasks do. I was thinking I could do something like this playbook below 
> to get what I want but it doesn't work either:
>
> ---
> ## 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
>
>
>
> In summary, I want to mount any device it finds ONLY if it is NTFS
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a95d009b-a01d-4ae7-804b-34abfcf577fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to