condition in playbook

38 views
Skip to first unread message

Thirumalai Raja A

unread,
Apr 17, 2024, 3:22:54 AM4/17/24
to ansible...@googlegroups.com
hi all,

below is my playbook ia m trying to add some variable in extra_vars: argument and based on the condition but its giving error can some guide me how can we mention this like if string 'oracle' is there in template_name variable i need to add some extra var otherwise it should not add.

    - name: Run PPS Patch
      template_run:
        awx_url: "{{ awx_url }}"
        username: "{{ username }}"
        password: "{{ password }}"
        scm_branch_for_agent: "{{ scm_branch_for_PPS }}"
        project_name: "{{ project_name }}"
        template_name: "{{ job_temp_name }}"
        customEE_name: "{{ customEE_name }}"
        inventory_name: "{{ inventory_name }}"
        ip_add: "{{ vm_ipv4_address }}"
        skip_tags: "{{ skip_tags }}"
        extra_vars:
          sat_client_id: "{{ sat_client_id }}"
          sat_client_secret: "{{ sat_client_secret }}"
          websec_prod_id: "{{ websec_prod_id }}"
          websec_prod_secret: "{{ websec_prod_secret }}"
          radius_secret: "{{ radius_secret }}"
          {% if oracle in job_temp_name %}
          ansible_distribution: "RedHat"
          patch_enablerepo: "value"
          standard_enablerepo: "value"
          {% endif %}
        playbook: "{{ patch_playbook }}"
      when: not is_pps_template or ('FAILED' in status_check.tie_patch)
      register: ppsstatus_check
      until: "'patch.yml completed successfully' in ppsstatus_check.output"
      retries: 3
      delay: 10
      tags:
       - pps_update
       - pps-patch

Dick Visser

unread,
Apr 17, 2024, 6:04:43 AM4/17/24
to ansible...@googlegroups.com
Hii,
I have never seen a task called "template_run", is this something
custom that you built?
> --
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALyK%3Dg-%3DUYkSKUZJ8CXSaBbAne0KoyVErx0LcEzZK2VCZ7Z7hg%40mail.gmail.com.

Todd Lewis

unread,
Apr 17, 2024, 10:01:36 AM4/17/24
to ansible...@googlegroups.com, uto...@gmail.com
I don't know what consumes this ("template_run"?), but you could try this:

        extra_vars:
          sat_client_id: "{{ sat_client_id }}"
          sat_client_secret: "{{ sat_client_secret }}"
          websec_prod_id: "{{ websec_prod_id }}"
          websec_prod_secret: "{{ websec_prod_secret }}"
          radius_secret: "{{ radius_secret }}"
          ansible_distribution: '{{ "RedHat" if "oracle" in job_temp_name else omit }}'
          patch_enablerepo: '{{ "value" if "oracle" in job_temp_name else omit }}'
          standard_enablerepo: '{{ "value" if "oracle" in job_temp_name else omit }}'

    but I don't really expect that to work.

You'd have to be pretty desperate to use this, but I would handle it using my "logical" filter:
    - name: Run PPS Patch
      template_run:
        awx_url: "{{ awx_url }}"
        username: "{{ username }}"
        password: "{{ password }}"
        scm_branch_for_agent: "{{ scm_branch_for_PPS }}"
        project_name: "{{ project_name }}"
        template_name: "{{ job_temp_name }}"
        customEE_name: "{{ customEE_name }}"
        inventory_name: "{{ inventory_name }}"
        ip_add: "{{ vm_ipv4_address }}"
        skip_tags: "{{ skip_tags }}"
        # requires "ansible-galaxy collection install utoddl.logical"
        extra_vars: "{{ extra_vars_ | utoddl.logical.logical }}"
        playbook: "{{ patch_playbook }}"
      when: not is_pps_template or ('FAILED' in status_check.tie_patch)
      vars:
        extra_vars_:
          sat_client_id: "{{ sat_client_id }}"
          sat_client_secret: "{{ sat_client_secret }}"
          websec_prod_id: "{{ websec_prod_id }}"
          websec_prod_secret: "{{ websec_prod_secret }}"
          radius_secret: "{{ radius_secret }}"
          <<tmp:
            - if:
              - "{{ 'oracle' in job_temp_name }}"
              - ansible_distribution: "RedHat"
              - patch_enablerepo: "value"
              - standard_enablerepo: "value"
--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALyK%3Dg-%3DUYkSKUZJ8CXSaBbAne0KoyVErx0LcEzZK2VCZ7Z7hg%40mail.gmail.com.

-- 
Todd

Brian Coca

unread,
Apr 17, 2024, 10:28:38 AM4/17/24
to ansible...@googlegroups.com
This won't work as playbooks themselves are not jinja2 evaluated, only
keys inside a play are

{% if oracle in job_temp_name %}
ansible_distribution: "RedHat"
patch_enablerepo: "value"
standard_enablerepo: "value"
{% endif %}

An alternative would be:
ansible_distribution: "{{ oracle in job_temp_name|
ternary('RedHat', omit) }}"
patch_enablerepo: "{{ oracle in job_temp_name|
ternary('value', omit) }}"
standard_enablerepo: "{{ oracle in job_temp_name|
ternary('value', omit) }}"

----------
Brian Coca (he/him/yo)

Thirumalai Raja A

unread,
Apr 17, 2024, 11:14:19 AM4/17/24
to ansible...@googlegroups.com, uto...@gmail.com
let me try this thanks,


Reply all
Reply to author
Forward
0 new messages