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

Access to Variable values from Extra CLI field #1053

Open
mmospanenko opened this issue Oct 13, 2022 · 11 comments
Open

Access to Variable values from Extra CLI field #1053

mmospanenko opened this issue Oct 13, 2022 · 11 comments

Comments

@mmospanenko
Copy link

mmospanenko commented Oct 13, 2022

Will be great to have ability to read values from UI Vars inside predefined Extra CLI Arguments template. For example, I want to ask a user what hosts he wants to deploy. We have all hosts inside inventory, but we should limit them for each run (-l arg).

How I see this implementation:
just add ability to use jinja2 template for this (Extra CLI) text field and access to variables.

  1. we add Hosts variable (hosts)
  2. inside the text field, we set ['-l', '{{ hosts }}']

That's all, use case covered.

image

@mmospanenko mmospanenko changed the title Read Variable value to pass it in Extra CLI template Access to Variable values from Extra CLI field Oct 13, 2022
@sw9493
Copy link

sw9493 commented Dec 9, 2022

I have done similar functionality just by creating a limit_to variable in Environment that maps to hosts: {{ limit_to }} in the playbooks. Seems to work well

@ansibleguy
Copy link
Contributor

Greetings!

Actually I don't think so - as those survey-vars seem to be translated into 'extra-vars' that cannot be referenced in the context of the command-execution. I read into the codebase a little, but did not manage to find out how they are handled.

I opened a specific feature-request for adding a --limit field: #1357

@mmospanenko
Copy link
Author

Solved using ansible_limit as variable, it works
image

@kyberorg
Copy link

@mmospanenko works like charm. Thanks a lot!

@stevmills
Copy link

stevmills commented Aug 15, 2023

@mmospanenko works like charm. Thanks a lot!

Hey! Could you say what version of Ansible you had success with? I am not having any luck specifying ansible_limit in a survey; also in testing, specifying -e ansible_limit=LIMIT at the CLI doesn't seem to work for me either.

@mmospanenko
Copy link
Author

mmospanenko commented Aug 15, 2023

Hey! Could you say what version of Ansible you had success with? I am not having any luck specifying ansible_limit in a survey; also in testing, specifying -e ansible_limit=LIMIT at the CLI doesn't seem to work for me either.

just add UI variable, no any changes required in CLI template (it transmits automatically as -e), only name (Name field is important). Version from Semaphore Docker, doesn't matter

more about this var https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-ansible_limit

@craigarms
Copy link

craigarms commented Aug 18, 2023

Hey! Could you say what version of Ansible you had success with? I am not having any luck specifying ansible_limit in a survey; also in testing, specifying -e ansible_limit=LIMIT at the CLI doesn't seem to work for me either.

just add UI variable, no any changes required in CLI template (it transmits automatically as -e), only name (Name field is important). Version from Semaphore Docker, doesn't matter

more about this var https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-ansible_limit

Hi,

I'm trying this out and heavily rely on limit for my playbooks, but this doesn't seem to be working for me, although the survey variable is passed.

12:07:42 AM
PLAYBOOK: server_automation_initial_setup.yml **********************************
12:07:42 AM
Positional arguments: playbooks/server_automation_initial_setup.yml
12:07:42 AM
verbosity: 4
12:07:42 AM
connection: smart
12:07:42 AM
timeout: 10
12:07:42 AM
become_method: sudo
12:07:42 AM
tags: ('all',)
12:07:42 AM
check: True
12:07:42 AM
inventory: ('/tmp/semaphore/repository_1_2/inventory.yml',)
12:07:42 AM
extra_vars: ('@/tmp/semaphore/access_key_649084140', '@/tmp/semaphore/access_key_289122401', '{"ansible_limit":"DOC01","semaphore_vars":{"task_details":{"id":17,"username":"admin"}}}', 'ansible_password=password')
12:07:42 AM
vault_password_files: ('/tmp/semaphore/access_key_572594559',)
12:07:42 AM
forks: 5
12:07:42 AM
1 plays in playbooks/server_automation_initial_setup.yml

I'm using the semaphore:lastest, wondering if this is an Ansible issue, or if this special variable isn't meant to be set in this way.

@mmospanenko
Copy link
Author

mmospanenko commented Aug 19, 2023

I don't see your input (all configs) so can't comment. Read the docs carefully and it should work. You should pass limit to Ansible

@stevmills
Copy link

Hey! Could you say what version of Ansible you had success with? I am not having any luck specifying ansible_limit in a survey; also in testing, specifying -e ansible_limit=LIMIT at the CLI doesn't seem to work for me either.

just add UI variable, no any changes required in CLI template (it transmits automatically as -e), only name (Name field is important). Version from Semaphore Docker, doesn't matter
more about this var https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#term-ansible_limit

Hi,

I'm trying this out and heavily rely on limit for my playbooks, but this doesn't seem to be working for me, although the survey variable is passed.

12:07:42 AM
PLAYBOOK: server_automation_initial_setup.yml **********************************
12:07:42 AM
Positional arguments: playbooks/server_automation_initial_setup.yml
12:07:42 AM
verbosity: 4
12:07:42 AM
connection: smart
12:07:42 AM
timeout: 10
12:07:42 AM
become_method: sudo
12:07:42 AM
tags: ('all',)
12:07:42 AM
check: True
12:07:42 AM
inventory: ('/tmp/semaphore/repository_1_2/inventory.yml',)
12:07:42 AM
extra_vars: ('@/tmp/semaphore/access_key_649084140', '@/tmp/semaphore/access_key_289122401', '{"ansible_limit":"DOC01","semaphore_vars":{"task_details":{"id":17,"username":"admin"}}}', 'ansible_password=password')
12:07:42 AM
vault_password_files: ('/tmp/semaphore/access_key_572594559',)
12:07:42 AM
forks: 5
12:07:42 AM
1 plays in playbooks/server_automation_initial_setup.yml

I'm using the semaphore:lastest, wondering if this is an Ansible issue, or if this special variable isn't meant to be set in this way.

@craigarms this is the same behavior I see. Ansible considers ansible_limit a "Magic Variable" which the docs explicitly state cannot be set directly by a user.

Taking Semaphore out of the equation altogether, I tested by attempting pass in ansible_limit at the CLI using extra vars (which is how Semaphore does it I believe) but that variable was still always undefined inside the playbook execution. The only time is gets defined is when I supply the --limit argument.

I understand this directly conflicts with the reports of success above, so I would love to know what I'm missing. (Or for issue #1357 to be implemented 😄).

@ansibleguy
Copy link
Contributor

The clean fix will be the 'limit form-field' feature: #1357

@jackivanov
Copy link

jackivanov commented Feb 21, 2024

Here's another workaround. You need to create srv survey variable

- name: Create limit inventory
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Assert limit
      run_once: yes
      assert:
        that:
          - 'srv is defined'
        fail_msg: 'srv variable must be present, e.g., `-e srv=xxx.local`'
        quiet: yes

    - name: Dynamic inventory
      ansible.builtin.add_host:
        hostname: '{{ srv }}'
        groups: dynamic_limit
      loop: "{{ srv.split(',') }}"
      when: srv is defined and srv != ''

- name: Dynamic limit
  hosts: dynamic_limit
  tasks:
    - debug:
      msg: "Distribution: {{ ansible_distribution }} {{ ansible_distribution_version }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants