Merge pull request #11081 from sean-m-sullivan/schedule_credentials

add credentials option to schedules
This commit is contained in:
Bianca Henderson
2021-09-16 13:19:58 -04:00
committed by GitHub
2 changed files with 186 additions and 71 deletions

View File

@@ -53,6 +53,11 @@ options:
- Inventory applied as a prompt, assuming job template prompts for inventory - Inventory applied as a prompt, assuming job template prompts for inventory
required: False required: False
type: str type: str
credentials:
description:
- List of credentials applied as a prompt, assuming job template prompts for credentials
type: list
elements: str
scm_branch: scm_branch:
description: description:
- Branch to use in job run. Project default used if blank. Only allowed if project allow_override field is set to true. - Branch to use in job run. Project default used if blank. Only allowed if project allow_override field is set to true.
@@ -147,6 +152,7 @@ def main():
description=dict(), description=dict(),
extra_data=dict(type='dict'), extra_data=dict(type='dict'),
inventory=dict(), inventory=dict(),
credentials=dict(type='list', elements='str'),
scm_branch=dict(), scm_branch=dict(),
job_type=dict(choices=['run', 'check']), job_type=dict(choices=['run', 'check']),
job_tags=dict(), job_tags=dict(),
@@ -169,6 +175,7 @@ def main():
description = module.params.get('description') description = module.params.get('description')
extra_data = module.params.get('extra_data') extra_data = module.params.get('extra_data')
inventory = module.params.get('inventory') inventory = module.params.get('inventory')
credentials = module.params.get('credentials')
scm_branch = module.params.get('scm_branch') scm_branch = module.params.get('scm_branch')
job_type = module.params.get('job_type') job_type = module.params.get('job_type')
job_tags = module.params.get('job_tags') job_tags = module.params.get('job_tags')
@@ -191,6 +198,13 @@ def main():
# Attempt to look up an existing item based on the provided data # Attempt to look up an existing item based on the provided data
existing_item = module.get_one('schedules', name_or_id=name) existing_item = module.get_one('schedules', name_or_id=name)
association_fields = {}
if credentials is not None:
association_fields['credentials'] = []
for item in credentials:
association_fields['credentials'].append(module.resolve_name_to_id('credentials', item))
# Create the data that gets sent for create and update # Create the data that gets sent for create and update
new_fields = {} new_fields = {}
if rrule is not None: if rrule is not None:
@@ -226,7 +240,12 @@ def main():
module.delete_if_needed(existing_item) module.delete_if_needed(existing_item)
elif state == 'present': elif state == 'present':
# If the state was present and we can let the module build or update the existing item, this will return on its own # If the state was present and we can let the module build or update the existing item, this will return on its own
module.create_or_update_if_needed(existing_item, new_fields, endpoint='schedules', item_type='schedule', associations={}) module.create_or_update_if_needed(
existing_item,
new_fields,
endpoint='schedules',
item_type='schedule',
associations=association_fields,)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -3,86 +3,182 @@
set_fact: set_fact:
test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}" test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
- name: generate random string for project - name: generate random string for schedule
set_fact: set_fact:
sched1: "AWX-Collection-tests-schedule-sched1-{{ test_id }}" sched1: "AWX-Collection-tests-schedule-sched1-{{ test_id }}"
cred1: "AWX-Collection-tests-schedule-cred1-{{ test_id }}"
proj1: "AWX-Collection-tests-schedule-proj-{{ test_id }}"
jt1: "AWX-Collection-tests-schedule-jt1-{{ test_id }}"
- name: Try to create without an rrule - block:
schedule: - name: Try to create without an rrule
name: "{{ sched1 }}" schedule:
state: present name: "{{ sched1 }}"
unified_job_template: "Demo Job Template" state: present
enabled: true unified_job_template: "Demo Job Template"
register: result enabled: true
ignore_errors: true register: result
ignore_errors: true
- assert: - assert:
that: that:
- result is failed - result is failed
- "'Unable to create schedule {{ sched1 }}' in result.msg" - "'Unable to create schedule {{ sched1 }}' in result.msg"
- name: Create with options that the JT does not support - name: Create with options that the JT does not support
schedule: schedule:
name: "{{ sched1 }}" name: "{{ sched1 }}"
state: present state: present
unified_job_template: "Demo Job Template" unified_job_template: "Demo Job Template"
rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1" rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1"
description: "This hopefully will not work" description: "This hopefully will not work"
extra_data: extra_data:
some: var some: var
inventory: Demo Inventory inventory: Demo Inventory
scm_branch: asdf1234 scm_branch: asdf1234
job_type: run job_type: run
job_tags: other_tags job_tags: other_tags
skip_tags: some_tags skip_tags: some_tags
limit: node1 limit: node1
diff_mode: true diff_mode: true
verbosity: 4 verbosity: 4
enabled: true enabled: true
register: result register: result
ignore_errors: true ignore_errors: true
- assert: - assert:
that: that:
- result is failed - result is failed
- "'Unable to create schedule {{ sched1 }}' in result.msg" - "'Unable to create schedule {{ sched1 }}' in result.msg"
- name: Build a real schedule - name: Build a real schedule
schedule: schedule:
name: "{{ sched1 }}" name: "{{ sched1 }}"
state: present state: present
unified_job_template: "Demo Job Template" unified_job_template: "Demo Job Template"
rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1" rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1"
register: result register: result
- assert: - assert:
that: that:
- result is changed - result is changed
- name: Rebuild the same schedule - name: Rebuild the same schedule
schedule: schedule:
name: "{{ sched1 }}" name: "{{ sched1 }}"
state: present state: present
unified_job_template: "Demo Job Template" unified_job_template: "Demo Job Template"
rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1" rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1"
register: result register: result
- assert: - assert:
that: that:
- result is not changed - result is not changed
- name: Disable a schedule - name: Create a Demo Project
schedule: project:
name: "{{ sched1 }}" name: "{{ proj1 }}"
state: present organization: Default
enabled: "false" allow_override: true
register: result state: present
scm_type: git
scm_url: https://github.com/ansible/ansible-tower-samples.git
- assert: - name: Create Credential1
that: credential:
- result is changed name: "{{ cred1 }}"
organization: Default
credential_type: Red Hat Ansible Automation Platform
register: cred1_result
- name: Delete the schedule - name: Create Job Template with all prompts
schedule: job_template:
name: "{{ sched1 }}" name: "{{ jt1 }}"
state: absent organization: Default
project: "{{ proj1 }}"
inventory: Demo Inventory
playbook: hello_world.yml
ask_variables_on_launch: true
ask_inventory_on_launch: true
ask_scm_branch_on_launch: true
ask_credential_on_launch: true
ask_job_type_on_launch: true
ask_tags_on_launch: true
ask_skip_tags_on_launch: true
ask_limit_on_launch: true
ask_diff_mode_on_launch: true
ask_verbosity_on_launch: true
job_type: run
state: present
register: result
- assert:
that:
- "result is changed"
- name: Create with options that the JT does support
schedule:
name: "{{ sched1 }}"
state: present
unified_job_template: "{{ jt1 }}"
rrule: "DTSTART:20191219T130551Z RRULE:FREQ=WEEKLY;INTERVAL=1;COUNT=1"
description: "This hopefully will not work"
extra_data:
some: var
inventory: Demo Inventory
scm_branch: asdf1234
credentials:
- "{{ cred1 }}"
job_type: run
job_tags: other_tags
skip_tags: some_tags
limit: node1
diff_mode: true
verbosity: 4
enabled: true
register: result
ignore_errors: true
- assert:
that:
- "result is changed"
- name: Disable a schedule
schedule:
name: "{{ sched1 }}"
state: present
enabled: "false"
register: result
- assert:
that:
- result is changed
always:
- name: Delete the schedule
schedule:
name: "{{ sched1 }}"
state: absent
- name: Delete the jt
job_template:
name: "{{ jt1 }}"
project: "{{ proj1 }}"
playbook: hello_world.yml
state: absent
- name: Delete the Project
project:
name: "{{ proj1 }}"
organization: Default
state: absent
scm_type: git
scm_url: https://github.com/ansible/ansible-tower-samples.git
register: result
- name: Delete Credential1
credential:
name: "{{ cred1 }}"
organization: Default
credential_type: Red Hat Ansible Automation Platform
state: absent