ansible / awx

AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Other
13.86k stars 3.4k forks source link

Setting same names to job template and its project causes tower_job_template to fail. #10506

Closed zaskan closed 11 months ago

zaskan commented 3 years ago
ISSUE TYPE
SUMMARY

Setting same names to job template and its project causes tower_job_template to fail. It produces the following output:

TASK [Create JT] *************************************************************************************************************************************************
task path: /home/tower/git/repo/roles/templates/tasks/main.yml:12
Tuesday 14 May 2021  10:19:34 +0100 (0:00:00.131)       0:00:00.131 **********
<tower-lab> ESTABLISH LOCAL CONNECTION FOR USER: MASKED
<tower-lab> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/.ansible_$USER/tmp `"&& mkdir /tmp/.ansible_$USER/tmp/ansible-tmp-1231231231.12-65456-234234344334432 && echo ansible-tmp-1231231231.12-65456-234234344334432="` echo /tmp/.ansible_$USER/tmp/ansible-tmp-1231231231.12-65456-234234344334432 `" ) && sleep 0'
<tower-lab> Attempting python interpreter discovery
<tower-lab> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<tower-lab> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
Using module file /home/tower/.ansible/collections/ansible_collections/ansible/tower/plugins/modules/tower_job_template.py
<tower-lab> PUT /home/tower/.ansible/tmp/ansible-local-63524Jyeuh1/tmp_f1AdN TO /tmp/.ansible_tower/tmp/ansible-tmp-1231231231.12-65456-234234344334432/AnsiballZ_tower_job_template.py
<tower-lab> EXEC /bin/sh -c 'chmod u+x /tmp/.ansible_tower/tmp/ansible-tmp-1231231231.12-65456-234234344334432/ /tmp/.ansible_tower/tmp/ansible-tmp-1231231231.12-65456-234234344334432/AnsiballZ_tower_job_template.py && sleep 0'
<tower-lab> EXEC /bin/sh -c '/usr/bin/python /tmp/.ansible_tower/tmp/ansible-tmp-1231231231.12-65456-234234344334432/AnsiballZ_tower_job_template.py && sleep 0'
<tower-lab> EXEC /bin/sh -c 'rm -f -r /tmp/.ansible_tower/tmp/ansible-tmp-1231231231.12-65456-234234344334432/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_ansible.tower.tower_job_template_payload_lqzUlT/ansible_ansible.tower.tower_job_template_payload.zip/ansible_collections/ansible/tower/plugins/module_utils/tower_api.py", line 132, in get_one
    new_data['or__id'] = int(name_or_id)
failed: [tower-lab] (item=PNAME) => changed=false
  ansible_facts:
    discovered_interpreter_python: /usr/bin/python
  ansible_loop_var: item
  invocation:
    module_args:
      allow_simultaneous: false
      ask_credential_on_launch: null
      ask_diff_mode_on_launch: null
      ask_inventory_on_launch: null
      ask_job_type_on_launch: null
      ask_limit_on_launch: null
      ask_scm_branch_on_launch: null
      ask_skip_tags_on_launch: null
      ask_tags_on_launch: null
      ask_variables_on_launch: null
      ask_verbosity_on_launch: null
      become_enabled: null
      concurrent_jobs_enabled: false
      credential: null
      credentials: null
      custom_virtualenv: /opt/venvs/TEST/
      description: PNAME
      diff_mode: null
      extra_vars: null
      force_handlers: false
      forks: null
      host_config_key: null
      inventory: test_inventory
      job_slice_count: 1
      job_tags: null
      job_type: null
      labels: null
      limit: null
      name: PNAME
      new_name: null
      notification_templates_error: null
      notification_templates_started: null
      notification_templates_success: null
      organization: null
      playbook: playbook.yml
      project: PNAME
      scm_branch: null
      skip_tags: null
      start_at_task: null
      state: present
      survey_enabled: null
      survey_spec: null
      timeout: 0
      tower_config_file: null
      tower_host: https://tower-lab/
      tower_oauthtoken: null
      tower_password: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
      tower_username: admin
      tower_verify_ssl: false
      use_fact_cache: null
      validate_certs: false
      vault_credential: test_cred
      verbosity: 0
      webhook_credential: null
      webhook_service: null
  item:
    key: PNAME
    value:
      concurrent_jobs_enabled: false
      custom_virtualenv: /opt/venvs/TEST/
      description: PNAME
      inventory: test_inventory
      playbook: playbook.yml
      project: PNAME
      vault_credential: TEST_CREDtest_cred
  msg: Request to api/v2/projects/?name=PNAME returned 0 items, expected 1
  query:
    name: PNAME
  response:
    json:
      count: 0
      next: null
      previous: null
      results: []
    status_code: 200
  total_results: 0
ENVIRONMENT
STEPS TO REPRODUCE

Create a project and assign it to a job template with the same name. Run the job template.

ADDITIONAL INFORMATION

CUSTOMER CASE

fosterseth commented 3 years ago

having trouble replicating on awx devel / ansible-devel, can you post a snippet from your playbook?

here is how I tested,

---
- name: playbook jt same name
  hosts: localhost
  connection: local
  gather_facts: False
  collections:
    - awx.awx

  tasks:
    - name: Delete project
      project:
        name: PNAME
        state: absent

    - name: Delete jt
      job_template:
        name: PNAME
        state: absent

    - name: Create a project
      project:
        name: PNAME
        copy_from: Demo Project
        scm_type: git
        description: here is a project
        organization: Default
        state: present

    - name: Create a jt
      job_template:
        name: PNAME
        copy_from: Demo Job Template
        job_type: run
        inventory: Demo Inventory
        project: PNAME
        organization: Default
        playbook: hello_world.yml
        state: present

    - name: Launch it
      job_launch:
        name: PNAME

image

zaskan commented 3 years ago

Hi, I have created an easier reproducer.

You need to create a project named "Test" sourcing this repo.

https://github.com/zaskan/test

Also, you need to create a job template with same name "Test" using previously created project "Test" and playbook.yaml. Configure your Automation Hub credentials in order to download the requirements file.

If you run the template you will get the following.

{
    "msg": "Request to /api/v2/unified_job_templates/?name=Test returned 2 items, expected 1",
    "query": {
        "name": "Test"
    },
    "response": {
        "status_code": 200,
        "json": {
            "count": 2,
            "next": null,
            "previous": null,
            "results": [
                {
                    "id": 12,
                    "type": "project",
                    "url": "/api/v2/projects/12/",
                    "related": {
                        "created_by": "/api/v2/users/1/",
                        "modified_by": "/api/v2/users/1/",
                        "last_job": "/api/v2/project_updates/41/",
                        "teams": "/api/v2/projects/12/teams/",
                        "playbooks": "/api/v2/projects/12/playbooks/",
                        "inventory_files": "/api/v2/projects/12/inventories/",
                        "update": "/api/v2/projects/12/update/",
                        "project_updates": "/api/v2/projects/12/project_updates/",
                        "scm_inventory_sources": "/api/v2/projects/12/scm_inventory_sources/",
                        "schedules": "/api/v2/projects/12/schedules/",
                        "activity_stream": "/api/v2/projects/12/activity_stream/",
                        "notification_templates_started": "/api/v2/projects/12/notification_templates_started/",
                        "notification_templates_success": "/api/v2/projects/12/notification_templates_success/",
                        "notification_templates_error": "/api/v2/projects/12/notification_templates_error/",
                        "access_list": "/api/v2/projects/12/access_list/",
                        "object_roles": "/api/v2/projects/12/object_roles/",
                        "copy": "/api/v2/projects/12/copy/",
                        "organization": "/api/v2/organizations/1/",
                        "last_update": "/api/v2/project_updates/41/"
                    },
                    "summary_fields": {
                        "organization": {
                            "id": 1,
                            "name": "Default",
                            "description": ""
                        },
                        "last_job": {
                            "id": 41,
                            "name": "Test",
                            "description": "",
                            "finished": "2021-07-07T07:53:22.868369Z",
                            "status": "successful",
                            "failed": false
                        },
                        "last_update": {
                            "id": 41,
                            "name": "Test",
                            "description": "",
                            "status": "successful",
                            "failed": false
                        },
                        "created_by": {
                            "id": 1,
                            "username": "admin",
                            "first_name": "",
                            "last_name": ""
                        },
                        "modified_by": {
                            "id": 1,
                            "username": "admin",
                            "first_name": "",
                            "last_name": ""
                        },
                        "object_roles": {
                            "admin_role": {
                                "description": "Can manage all aspects of the project",
                                "name": "Admin",
                                "id": 52
                            },
                            "use_role": {
                                "description": "Can use the project in a job template",
                                "name": "Use",
                                "id": 53
                            },
                            "update_role": {
                                "description": "May update the project",
                                "name": "Update",
                                "id": 54
                            },
                            "read_role": {
                                "description": "May view settings for the project",
                                "name": "Read",
                                "id": 55
                            }
                        },
                        "user_capabilities": {
                            "edit": true,
                            "delete": true,
                            "start": true,
                            "schedule": true,
                            "copy": true
                        }
                    },
                    "created": "2021-07-07T07:00:08.973227Z",
                    "modified": "2021-07-07T07:14:22.503496Z",
                    "name": "Test",
                    "description": "",
                    "local_path": "_12__Test",
                    "scm_type": "git",
                    "scm_url": "https://github.com/zaskan/test.git",
                    "scm_branch": "",
                    "scm_refspec": "",
                    "scm_clean": false,
                    "scm_delete_on_update": true,
                    "credential": null,
                    "timeout": 0,
                    "scm_revision": "f4100cf9e152ddad99e3b636e210cfc910ee19ae",
                    "last_job_run": "2021-07-07T07:53:22.868369Z",
                    "last_job_failed": false,
                    "next_job_run": null,
                    "status": "successful",
                    "organization": 1,
                    "scm_update_on_launch": true,
                    "scm_update_cache_timeout": 0,
                    "allow_override": false,
                    "custom_virtualenv": null,
                    "last_update_failed": false,
                    "last_updated": "2021-07-07T07:53:22.868369Z"
                },
                {
                    "id": 13,
                    "type": "job_template",
                    "url": "/api/v2/job_templates/13/",
                    "related": {
                        "created_by": "/api/v2/users/1/",
                        "modified_by": "/api/v2/users/1/",
                        "labels": "/api/v2/job_templates/13/labels/",
                        "inventory": "/api/v2/inventories/1/",
                        "project": "/api/v2/projects/12/",
                        "organization": "/api/v2/organizations/1/",
                        "credentials": "/api/v2/job_templates/13/credentials/",
                        "current_job": "/api/v2/jobs/40/",
                        "last_job": "/api/v2/jobs/38/",
                        "jobs": "/api/v2/job_templates/13/jobs/",
                        "schedules": "/api/v2/job_templates/13/schedules/",
                        "activity_stream": "/api/v2/job_templates/13/activity_stream/",
                        "launch": "/api/v2/job_templates/13/launch/",
                        "webhook_key": "/api/v2/job_templates/13/webhook_key/",
                        "webhook_receiver": "",
                        "notification_templates_started": "/api/v2/job_templates/13/notification_templates_started/",
                        "notification_templates_success": "/api/v2/job_templates/13/notification_templates_success/",
                        "notification_templates_error": "/api/v2/job_templates/13/notification_templates_error/",
                        "access_list": "/api/v2/job_templates/13/access_list/",
                        "survey_spec": "/api/v2/job_templates/13/survey_spec/",
                        "object_roles": "/api/v2/job_templates/13/object_roles/",
                        "instance_groups": "/api/v2/job_templates/13/instance_groups/",
                        "slice_workflow_jobs": "/api/v2/job_templates/13/slice_workflow_jobs/",
                        "copy": "/api/v2/job_templates/13/copy/"
                    },
                    "summary_fields": {
                        "organization": {
                            "id": 1,
                            "name": "Default",
                            "description": ""
                        },
                        "inventory": {
                            "id": 1,
                            "name": "Demo Inventory",
                            "description": "",
                            "has_active_failures": true,
                            "total_hosts": 1,
                            "hosts_with_active_failures": 1,
                            "total_groups": 0,
                            "has_inventory_sources": false,
                            "total_inventory_sources": 0,
                            "inventory_sources_with_failures": 0,
                            "organization_id": 1,
                            "kind": ""
                        },
                        "project": {
                            "id": 12,
                            "name": "Test",
                            "description": "",
                            "status": "successful",
                            "scm_type": "git"
                        },
                        "last_job": {
                            "id": 38,
                            "name": "Test",
                            "description": "",
                            "finished": "2021-07-07T07:52:43.983784Z",
                            "status": "failed",
                            "failed": true
                        },
                        "last_update": {
                            "id": 38,
                            "name": "Test",
                            "description": "",
                            "status": "failed",
                            "failed": true
                        },
                        "current_update": {
                            "id": 40,
                            "name": "Test",
                            "description": "",
                            "status": "running",
                            "failed": false
                        },
                        "current_job": {
                            "id": 40,
                            "name": "Test",
                            "description": "",
                            "status": "running",
                            "failed": false
                        },
                        "created_by": {
                            "id": 1,
                            "username": "admin",
                            "first_name": "",
                            "last_name": ""
                        },
                        "modified_by": {
                            "id": 1,
                            "username": "admin",
                            "first_name": "",
                            "last_name": ""
                        },
                        "object_roles": {
                            "admin_role": {
                                "description": "Can manage all aspects of the job template",
                                "name": "Admin",
                                "id": 56
                            },
                            "execute_role": {
                                "description": "May run the job template",
                                "name": "Execute",
                                "id": 57
                            },
                            "read_role": {
                                "description": "May view settings for the job template",
                                "name": "Read",
                                "id": 58
                            }
                        },
                        "user_capabilities": {
                            "edit": true,
                            "delete": true,
                            "start": true,
                            "schedule": true,
                            "copy": true
                        },
                        "labels": {
                            "count": 0,
                            "results": []
                        },
                        "recent_jobs": [
                            {
                                "id": 40,
                                "status": "running",
                                "finished": null,
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 38,
                                "status": "failed",
                                "finished": "2021-07-07T07:52:43.983784Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 36,
                                "status": "failed",
                                "finished": "2021-07-07T07:51:34.457730Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 34,
                                "status": "failed",
                                "finished": "2021-07-07T07:50:16.155347Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 26,
                                "status": "failed",
                                "finished": "2021-07-07T07:35:52.556898Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 24,
                                "status": "failed",
                                "finished": "2021-07-07T07:33:18.871046Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 13,
                                "status": "failed",
                                "finished": "2021-07-07T07:14:33.650837Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 12,
                                "status": "failed",
                                "finished": "2021-07-07T07:14:16.275078Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 11,
                                "status": "failed",
                                "finished": "2021-07-07T07:13:52.999169Z",
                                "canceled_on": null,
                                "type": "job"
                            },
                            {
                                "id": 10,
                                "status": "failed",
                                "finished": "2021-07-07T07:12:58.708359Z",
                                "canceled_on": null,
                                "type": "job"
                            }
                        ],
                        "credentials": [
                            {
                                "id": 5,
                                "name": "Tower",
                                "description": "",
                                "kind": "tower",
                                "cloud": true
                            }
                        ]
                    },
                    "created": "2021-07-07T07:00:39.723758Z",
                    "modified": "2021-07-07T07:53:09.790720Z",
                    "name": "Test",
                    "description": "",
                    "job_type": "run",
                    "inventory": 1,
                    "project": 12,
                    "playbook": "playbook.yaml",
                    "scm_branch": "",
                    "forks": 0,
                    "limit": "",
                    "verbosity": 3,
                    "extra_vars": "",
                    "job_tags": "",
                    "force_handlers": false,
                    "skip_tags": "",
                    "start_at_task": "",
                    "timeout": 0,
                    "use_fact_cache": false,
                    "organization": 1,
                    "last_job_run": "2021-07-07T07:52:43.983784Z",
                    "last_job_failed": true,
                    "next_job_run": null,
                    "status": "running",
                    "host_config_key": "",
                    "ask_scm_branch_on_launch": false,
                    "ask_diff_mode_on_launch": false,
                    "ask_variables_on_launch": false,
                    "ask_limit_on_launch": false,
                    "ask_tags_on_launch": false,
                    "ask_skip_tags_on_launch": false,
                    "ask_job_type_on_launch": false,
                    "ask_verbosity_on_launch": false,
                    "ask_inventory_on_launch": false,
                    "ask_credential_on_launch": false,
                    "survey_enabled": false,
                    "become_enabled": false,
                    "diff_mode": false,
                    "allow_simultaneous": false,
                    "custom_virtualenv": null,
                    "job_slice_count": 1,
                    "webhook_service": "",
                    "webhook_credential": null
                },
                "...more results snipped..."
            ]
        }
    },
    "total_results": 2,
    "invocation": {
        "module_args": {
            "name": "Schedule 1",
            "state": "present",
            "unified_job_template": "Test",
            "rrule": "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1",
            "tower_host": "https://127.0.0.1",
            "tower_username": "admin",
            "tower_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "validate_certs": false,
            "tower_oauthtoken": "",
            "tower_config_file": null,
            "new_name": null,
            "description": null,
            "extra_data": null,
            "inventory": null,
            "scm_branch": null,
            "job_type": null,
            "job_tags": null,
            "skip_tags": null,
            "limit": null,
            "diff_mode": null,
            "verbosity": null,
            "enabled": null
        }
    },
    "_ansible_no_log": false,
    "changed": false
}
fosterseth commented 3 years ago

In your playbook,

      ansible.tower.tower_schedule:
        name: "Schedule 1"
        state: present
        unified_job_template: "Test"

I see, yeah the module will call unified_job_template_id = module.resolve_name_to_id('unified_job_templates', unified_job_template)

which will return 2 entries, and will fail.

I think ideally we could provide an id for this module option, then you could use the lookup plugin to get the id of the "Test" JT

@AlanCoding in collection do we have a pattern where users can provide either a name or an id?

rwxd commented 2 years ago

Any updates on this? Sadly, I have the same problem. An Inventory will also be found by the unfied_job_template API entry point.

AlanCoding commented 2 years ago

Yes, you can use the lookup plugin to lookup by whatever search you need and have that return an id, which will be respected.

AlanCoding commented 11 months ago

Going to close this issue because the proposal is unclear, the lookup_plugin workaround works, and we have other issues that are more direct about improvements to name-based lookups.