Answers: <p>Q: <em><strong>&quot;If the variable item.ppsize is not set exclude that parameter.&quot;</strong></em></p>
  <p>A: Set the default value to the special variable <a href="https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#making-variables-optional" rel="nofollow noreferrer"><em>omit</em></a></p>
  <pre class="lang-yaml prettyprint-override"><code>    pp_size: &quot;{{ item.ppsize | default(omit) }}&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-28T18:38:02.837Z
Answers: 2024-04-18T05:18:22.503Z
Answers: 2024-04-18T05:18:22.503Z
Answers: Vladimir Botka
Body: <p>Is there a way to exclude a parameter entirely if a variable is <em>not</em> set?  I do not want to use a default value for this, I simply do not way the parameter referenced at all.</p>
  
  <p>For example,</p>
  
  <pre><code>---
  - name: manage_aix_lvm | creating new VG
    # https://docs.ansible.com/ansible/latest/modules/aix_lvg_module.html#aix-lvg-module
    aix_lvg:
      force: "{{ item.force | default('no') }}"
      pp_size: "{{ item.ppsize }}"
      pvs: "{{ item.disks | join(',') }}"
      vg: "{{ item.vgname }}"
      state: present
    become: true
    loop: "{{ lvm_groups }}"
  
  </code></pre>
  
  <p>In the event that variable item.ppsize is <em>not</em> set I want to exclude that entire parameter from being included.  The reason for this is that this is an optional parameter, and if not set, the Operating System will automatically calculate the value.</p>
  
  <p>Is this possible within a single task?  Or will I need to create multiple Tasks/Resources to check if item.ppsize is set and redirect to different tasks/resources?</p>
  
  <p>Thanks.</p>
  
Tags: <ansible>
Title: Ansible: Exclude a parameter if variable is not set
UserDisplayName: Dave L
ThreadText: # Ansible: Exclude a parameter if variable is not set
  
  (Asked by: username_0 on 2019-06-28)
  
  Is there a way to exclude a parameter entirely if a variable is *not* set? I do not want to use a default value for this, I simply do not way the parameter referenced at all.
  
  For example,
  
  ```
  ---
  - name: manage_aix_lvm | creating new VG
    # https://docs.ansible.com/ansible/latest/modules/aix_lvg_module.html#aix-lvg-module
    aix_lvg:
      force: "{{ item.force | default('no') }}"
      pp_size: "{{ item.ppsize }}"
      pvs: "{{ item.disks | join(',') }}"
      vg: "{{ item.vgname }}"
      state: present
    become: true
    loop: "{{ lvm_groups }}"
  
  ```
  
  In the event that variable item.ppsize is *not* set I want to exclude that entire parameter from being included. The reason for this is that this is an optional parameter, and if not set, the Operating System will automatically calculate the value.
  
  Is this possible within a single task? Or will I need to create multiple Tasks/Resources to check if item.ppsize is set and redirect to different tasks/resources?
  
  Thanks.
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-28. Score: 12 | Accepted answer
  
  Q: ***"If the variable item.ppsize is not set exclude that parameter."***
  
  A: Set the default value to the special variable [*omit*](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#making-variables-optional)
  
  ```yaml
      pp_size: "{{ item.ppsize | default(omit) }}"
  
  ```
  
  
  ---
  
  