Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with variable brackets inside any string in extra_vars #868

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

przemkalit
Copy link
Contributor

What does this PR do?

I am introducing this change to resolve the bug with brackets inside the extra_vars, I didn't find any other solution for this issue.

How should this be tested?

  1. Create a job template or workflow with extra_vars that contains:
    url: 'https://{{ ip }}'
  2. Export the object with filetree_create.
  3. The extra_vars of exported object should look like this:
extra_vars:
  "url": "https://{% raw %}{{{% endraw %} ip {% raw %}}}{% endraw %}'

Is there a relevant Issue open for this?

resolves #856

Other Relevant info, PRs, etc

N/A

Copy link
Contributor

@ivarmu ivarmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this jinja code, I'd prefer doing like follows:

regex_replace('(^[^:]*): (.*){{', '\1: !unsafe \2{{')

This is generating the following output, which should work as well, and leaves the generated yaml file more friendly:

cat example.yaml 
---
- name: play all
  hosts: a
  gather_facts: false
  vars:
    input: !unsafe |
      variable_name: 'https://{{ ip }}'
  tasks:
    - debug:
        msg:
          - "ORIGINAL: {{ input }}"
          - |
            MODIFIED: {{ input | regex_replace('(^[^:]*): (.*){{', '\1: !unsafe \2{{') }}
...

ansible-playbook -i inventory.yaml example.yaml

PLAY [play all] ******************************************************************************************************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [hosta] => {
    "msg": [
        "ORIGINAL: variable_name: 'https://{{ ip }}'\n",
        "MODIFIED: variable_name: !unsafe 'https://{{ ip }}'\n"
    ]
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
hosta                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

@przemkalit
Copy link
Contributor Author

Instead of adding this jinja code, I'd prefer doing like follows:

regex_replace('(^[^:]*): (.*){{', '\1: !unsafe \2{{')

This is generating the following output, which should work as well, and leaves the generated yaml file more friendly:

cat example.yaml 
---
- name: play all
  hosts: a
  gather_facts: false
  vars:
    input: !unsafe |
      variable_name: 'https://{{ ip }}'
  tasks:
    - debug:
        msg:
          - "ORIGINAL: {{ input }}"
          - |
            MODIFIED: {{ input | regex_replace('(^[^:]*): (.*){{', '\1: !unsafe \2{{') }}
...
ansible-playbook -i inventory.yaml example.yaml

PLAY [play all] ******************************************************************************************************************************************************************************************************************************

TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [hosta] => {
    "msg": [
        "ORIGINAL: variable_name: 'https://{{ ip }}'\n",
        "MODIFIED: variable_name: !unsafe 'https://{{ ip }}'\n"
    ]
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
hosta                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Ok, I've pushed the change to your suggestion.

Add `multiline=True` to treat multiple lines
Add `multiline=True` to treat multiple lines
Copy link
Contributor

@ivarmu ivarmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ivarmu ivarmu enabled auto-merge (squash) July 16, 2024 13:21
@ivarmu ivarmu merged commit 9f659e6 into redhat-cop:devel Jul 23, 2024
10 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filetree_create - extra_vars export issue
3 participants