skip to Main Content

I have a playbook to get all disks letter configured on my server and I need a task to verify if extra var letter is on the list.
For example I need to check if "F" is on the json data below.
Could you please help me on the best best syntax?

Thanks

 {
        "disks_drives_letter": [
            [
                "C"
            ],
            [
                "D"
            ],
            [
                "E"
            ],
            []
        ]
    }

3

Answers


  1. Chosen as BEST ANSWER

    | flatten help me thanks @vladimir-botka

    - name: Get all disks letter from the disks infos
          set_fact:
            disks_drives_letters: "{{ win_disk_facts | json_query(query) | flatten  }}"     
        
    - name: Check if disk_letter is used on server    fail:
        msg: "The disk letter already exist on the VM"   when:  '"{{ drive_letter }}" in "{{ disks_drives_letters}}"'
    

  2. You can use setup module to get your host information like disks. For more information about the setup module https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html

    Example of playbook:

    - hosts: localhost
      tasks:
      gather_facts: false
      vars:
        my_disk_drives: ['sda', 'sdb']
      tasks:
        - name: Collect host hardware information
          setup:
            gather_subset:
              - hardware
    
        - name: Output if disk exist
          debug:
            msg: "{{ item }} exists" 
          loop: "{{ my_disk_drives }}"
          when: item in hostvars[inventory_hostname].ansible_devices.keys() | list
    
        - name: Output if disks does not exist
          debug:
            msg: "{{ item }} does not exist" 
          loop: "{{ my_disk_drives }}"
          when: not item in hostvars[inventory_hostname].ansible_devices.keys() | list
    

    Output:

    TASK [Output if disk exist] 
    ok: [localhost] => (item=sda) => {
        "msg": "sda exists"
    }
    skipping: [localhost] => (item=sdb) 
    
    TASK [Output if disks does not exist] 
    skipping: [localhost] => (item=sda) 
    ok: [localhost] => (item=sdb) => {
        "msg": "sdb does not exist"
    }
    
    
    Login or Signup to reply.
  3. Use filters intersect and difference, and declare the lists

      my_disks_exist: "{{ ansible_devices.keys()|intersect(my_disks) }}"
      my_disks_not_exist: "{{ my_disks|difference(my_disks_exist) }}"
    

    Example of a complete playbook for testing

    - hosts: localhost
    
      vars:
    
        my_disks: [sda, sdb, sdc]
        my_disks_exist: "{{ ansible_devices.keys()|intersect(my_disks) }}"
        my_disks_not_exist: "{{ my_disks|difference(my_disks_exist) }}"
        
    
      tasks:
    
        - setup:
            gather_subset: devices
        - debug:
            var: ansible_devices.keys()
        - debug:
            var: my_disks_exist
        - debug:
            var: my_disks_not_exist
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search