user12377632
user12377632

Reputation: 23

Ansible assert module to loop through each value in nested dict

I'm trying to write an Ansible task to check variables (name, online,storage (blobStoreName, strictContentTypeValidation and writePolicy) from hosted, name, online,storage (blobStoreName, strictContentTypeValidation and writePolicy), proxy (remoteUrl, contentMaxAge, metadataMaxAge) etc ) to make sure all the variables are defined using assert module but i'm struggling to loop through the below nested dict.

Is it possible to loop through below vars and confirm all variables defined. I would really appreciate any suggestions or help.

repo_type:
  hosted:
    data:
      - name: hosted_repo1
        online: true
        storage:
          blobStoreName: default
          strictContentTypeValidation: true
          writePolicy: allow_once
      - name: hosted_repo2
        online: true
        storage:
          blobStoreName: default
          strictContentTypeValidation: true
          writePolicy: allow_once
  proxy:
    data:
      - name: proxy_repo
        online: true
        storage:
          blobStoreName: default
          strictContentTypeValidation: true
          writePolicy: allow_once
        proxy:
          remoteUrl: https://registry.npmjs.org
          contentMaxAge: 1440
          metadataMaxAge: 1440
        negativeCache:
          enabled: false
          timeToLive”: 1440
        httpClient:
          blocked: false,
          autoBlock: false
  group:
    data:
      - name: group_repo1
        online: true
        storage:
          blobStoreName: default
          strictContentTypeValidation: true
          writePolicy: allow_once
        group:
          groupMembers:
              - memeber1
              - memeber2
Sample task:
- name: check required vars present
  assert:
    that: 
      - "{{ item.1.name }} is defined"
  loop: "{{ repo_type | dict2items | subelements('value.data') }}"

Upvotes: 1

Views: 4387

Answers (1)

Antoni Pàmies
Antoni Pàmies

Reputation: 186

in order to check variables you can do this:

---
- hosts: localhost
  gather_facts: false

  vars:
    repo_type:
      hosted:
        data:
          - name: hosted_repo1
            online: true
            storage:
              blobStoreName: default
              strictContentTypeValidation: true
              writePolicy: allow_once
          - name: hosted_repo2
            online: true
            storage:
              blobStoreName: default
              strictContentTypeValidation: true
              writePolicy: allow_once
      proxy:
        data:
          - name: proxy_repo
            online: true
            storage:
              blobStoreName: default
              strictContentTypeValidation: true
              writePolicy: allow_once
            proxy:
              remoteUrl: https://registry.npmjs.org
              contentMaxAge: 1440
              metadataMaxAge: 1440
            negativeCache:
              enabled: false
              timeToLive”: 1440
            httpClient:
              blocked: false,
              autoBlock: false
      group:
        data:
          - name: group_repo1
            online: true
            storage:
              blobStoreName: default
              strictContentTypeValidation: true
              writePolicy: allow_once
            group:
              groupMembers:
                  - memeber1
                  - memeber2

  tasks:
    - name: CHECK_VAR | Check for duplicate names
      assert:
        that:
          - item.value.data
              | map( attribute = 'name' )
              | list
              | length
            ==
            item.value.data
              | map( attribute = 'name' )
              | list
              | unique
              | length
        quiet: yes
        fail_msg: >
          There are duplicate names in hosted data
      loop: "{{ repo_type | dict2items }}"

    - include_tasks: asserts.yml
      vars:
        item_check: "{{ item.1 }}"
        item_type: "{{ item.0.key }}"
      loop: "{{ repo_type | dict2items | subelements('value.data') }}"

the file asserts.yml contains:

---
- name: BLOCK_CHECK_VAR | Common checks
  block:
    - name: CHECK_VAR | {{ item_type }} - Checks for attribute name
      assert:
        that:
          - item_check.name is defined
          - item_check.name is string
          - item_check.name | length > 0
        quiet: yes
        fail_msg: >
          Attribute name is not defined, or is not string or ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute online
      assert:
        that:
          - item_check.online is defined
          - item_check.online | type_debug == 'bool'
        quiet: yes
        fail_msg: >
          Attribute online is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute storage
      assert:
        that:
          - item_check.storage is defined
          - item_check.storage | type_debug == 'dict'
        quiet: yes
        fail_msg: >
          Attribute storage is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute storage.blobStoreName
      vars:
        valid_blobStoreName:
          - default
      assert:
        that:
          - item_check.storage.blobStoreName is defined
          - item_check.storage.blobStoreName is string
          - item_check.storage.blobStoreName in valid_blobStoreName
        quiet: yes
        fail_msg: >
          Attribute storage.blobStoreName is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute storage.strictContentTypeValidation
      assert:
        that:
          - item_check.storage.strictContentTypeValidation is defined
          - item_check.storage.strictContentTypeValidation | type_debug == 'bool'
        quiet: yes
        fail_msg: >
          Attribute storage.strictContentTypeValidation is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute storage.writePolicy
      vars:
        valid_writePolicy:
          - allow_once
      assert:
        that:
          - item_check.storage.writePolicy is defined
          - item_check.storage.writePolicy is string
          - item_check.storage.writePolicy in valid_writePolicy
        quiet: yes
        fail_msg: >
          Attribute storage.writePolicy is not defined ....

  when:
    - item_type == "hosted" or item_type == "group" or item_type == "proxy"

- name: BLOCK_CHECK_VAR | Specific group checks
  block:
    - name: CHECK_VAR | {{ item_type }} - Checks for attribute group
      assert:
        that:
          - item_check.group is defined
          - item_check.group | type_debug == 'dict'
        quiet: yes
        fail_msg: >
          Attribute group is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for attribute groupMembers
      assert:
        that:
          - item_check.group.groupMembers is defined
          - item_check.group.groupMembers | type_debug == 'list'
          - item_check.group.groupMembers | length > 0
        quiet: yes
        fail_msg: >
          Attribute group.groupMembers is not defined ....

    - name: CHECK_VAR | {{ item_type }} - Checks for elements of attribute groupMembers
      assert:
        that:
          - myitem is string
          - myitem | length > 1
        quiet: yes
        fail_msg: >
          Element "{{ myitem }}" of attribute group.groupMembers is not defined ....
      loop: "{{ item_check.group.groupMembers }}"
      loop_control:
        loop_var: myitem
  when:
    - item_type == "group"

etc...
etc...
(missing the asserts specifics when item_type == "proxy")

Upvotes: 2

Related Questions