From: Gaƫtan <36162164+gaetanfl@users.noreply.github.com> Date: Wed, 12 Sep 2018 13:54:31 +0000 (+0200) Subject: Merge pull request #1 from gaetanfl/first_version X-Git-Tag: v0.1 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=56d88d5730d457ac24f9aa373b71588b433f4196;hp=1af6a8441cbbe29f8c30b1279400d86f28651912;p=github%2Ffretlink%2Fansible-rundeck-jobs.git Merge pull request #1 from gaetanfl/first_version first working version --- diff --git a/README.md b/README.md index 9f78f6c..618efd9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,47 @@ -# 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 +------------ + +* `jmespath` python module + +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 + vars: + rundeck_api_url: rundeck.example.com + rundeck_project: test + rundeck_jobs_path: /tmp/rundeck-jobs + roles: + - 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/requirements.txt b/requirements.txt new file mode 100644 index 0000000..45c1e03 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +jmespath diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..3d41031 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Include rundesk tasks + include_tasks: rundeck.yml + tags: + - rundeck-jobs 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