semaphoreui / semaphore

Modern UI for Ansible, Terraform, OpenTofu, PowerShell and other DevOps tools.
https://semaphoreui.com
MIT License
10.43k stars 1.05k forks source link

Access to Variable values from Extra CLI field #1053

Open mmospanenko opened 1 year ago

mmospanenko commented 1 year ago

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
sw9493 commented 1 year ago

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 commented 1 year ago

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: https://github.com/ansible-semaphore/semaphore/issues/1357

mmospanenko commented 1 year ago

Solved using ansible_limit as variable, it works

image
kyberorg commented 1 year ago

@mmospanenko works like charm. Thanks a lot!

stevmills commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

The clean fix will be the 'limit form-field' feature: https://github.com/ansible-semaphore/semaphore/issues/1357

jackivanov commented 7 months ago

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 }}"