From 030dd5c56410a21fc67e05e94f597c8bb255b7ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20Duchaussois?= Date: Wed, 12 Sep 2018 10:39:01 +0200 Subject: [PATCH] first working version --- README.md | 45 +++++++++++++++++++++++++++++++++++-- ansible.cfg | 2 ++ defaults/main.yml | 3 +++ handlers/main.yml | 2 ++ meta/main.yml | 57 +++++++++++++++++++++++++++++++++++++++++++++++ tasks/main.yml | 5 +++++ tasks/rundeck.yml | 47 ++++++++++++++++++++++++++++++++++++++ tests/inventory | 2 ++ tests/test.yml | 8 +++++++ vars/main.yml | 2 ++ 10 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 ansible.cfg create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 tasks/rundeck.yml create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md index 9f78f6c..2304d75 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,43 @@ -# ansible-rundeck-jobs -Synchronize rundeck jobs from a dir with a rundeck project +Rundeck jobs +========= + +This role synchronize a directory containing yaml definition of jobs with a rundeck project + +Requirements +------------ + +None + +Role Variables +-------------- + +* `rundeck_jobs_path` path of the directory containing the job definition (mandatory). +* `rundeck_project` name of the rundeck project (mandatory). +* `rundeck_api_url` base url of api (mandatory). +* `rundeck_api_token` the authentification token (mandatory). +* `rundeck_api_version` api version supported by rundeck server. Default to 26. +* `rundeck_remove_missing` Whether to delete jobs present in rundeck and not in file. Defaults to true. + +Dependencies +------------ + +None + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: rundeck-jobs, rundeck_api_url: rundeck.example.com, rundeck_project: test, rundeck_jobs_path: /tmp/rundeck-jobs } + +License +------- + +TBD + +Author Information +------------------ + +FretLink, Love and Truck diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..6462621 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,2 @@ +[defaults] +roles_path=../ diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..87958ec --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +rundeck_api_version: 26 +rundeck_remove_missing: true diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..0cdf3ee --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for ansible-rundeck-jobs \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..0e695af --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,57 @@ +galaxy_info: + author: FretLink + description: Love and Truck + company: FretLink + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: TBD + + min_ansible_version: 2.5 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # Optionally specify the branch Galaxy will use when accessing the GitHub + # repo for this role. During role install, if no tags are available, + # Galaxy will use this branch. During import Galaxy will access files on + # this branch. If Travis integration is configured, only notifications for this + # branch will be accepted. Otherwise, in all cases, the repo's default branch + # (usually master) will be used. + #github_branch: + + # + # platforms is a list of platforms, and each platform has a name and a list of versions. + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..ece8bb9 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Include rundesk tasks + include_tasks: rundeck.yml + tags: + - rundeck diff --git a/tasks/rundeck.yml b/tasks/rundeck.yml new file mode 100644 index 0000000..642abd6 --- /dev/null +++ b/tasks/rundeck.yml @@ -0,0 +1,47 @@ +- name: Create rundeck file list + find: + path: "{{ rundeck_jobs_path }}" + patterns: "*.yaml" + delegate_to: localhost + register: rundeck_jobs_files + +- name: Create rundeck jobs + uri: + url: "{{rundeck_api_url }}/{{rundeck_api_version}}/project/{{ rundeck_project }}/jobs/import" + method: POST + return_content: true + body_format: raw + body: fileformat=yaml&dupeOption=update&xmlBatch={{ lookup('file', item.path) | urlencode }} + headers: + Accept: application/json + Content-Type: application/x-www-form-urlencoded; charset=utf-8 + X-Rundeck-Auth-Token: "{{ rundeck_api_token }}" + register: rundeck_create_jobs + with_items: "{{ rundeck_jobs_files.files}}" + +- name: Get all jobs + uri: + url: "{{rundeck_api_url }}/{{rundeck_api_version}}/project/{{ rundeck_project }}/jobs" + method: GET + headers: + Accept: application/json + X-Rundeck-Auth-Token: "{{ rundeck_api_token }}" + register: rundeck_existing_jobs + when: rundeck_remove_missing + +- name: "Prepare list of jobs to remove" + set_fact: + rundeck_created_ids: "{{ rundeck_create_jobs.results | map(attribute='json') | map('json_query','succeeded[*].id') | flatten | list }}" + rundeck_jobs_ids: "{{ rundeck_existing_jobs.json | map(attribute='id') | list }}" + when: rundeck_remove_missing + +- name: "Remove jobs not in the directory" + uri: + url: "{{rundeck_api_url }}/{{rundeck_api_version}}/job/{{ item }}" + method: DELETE + headers: + Accept: application/json + X-Rundeck-Auth-Token: "{{ rundeck_api_token }}" + status_code: [204] + with_items: "{{ rundeck_jobs_ids | difference(rundeck_created_ids) }}" + when: rundeck_remove_missing diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..25ef811 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,8 @@ +--- +- hosts: localhost + roles: + - role: ansible-rundeck-jobs + rundeck_api_url: https://rundeck.example.org + rundeck_project: Test + rundeck_api_token: token + rundeck_jobs_path: tests/rundeck_files diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..add68e1 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for ansible-rundeck-jobs \ No newline at end of file -- 2.41.0