-# 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
--- /dev/null
+[defaults]
+roles_path=../
--- /dev/null
+---
+rundeck_api_version: 26
+rundeck_remove_missing: true
--- /dev/null
+---
+# handlers file for ansible-rundeck-jobs
\ No newline at end of file
--- /dev/null
+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.
--- /dev/null
+---
+- name: Include rundesk tasks
+ include_tasks: rundeck.yml
+ tags:
+ - rundeck
--- /dev/null
+- 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
--- /dev/null
+localhost
+
--- /dev/null
+---
+- 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
--- /dev/null
+---
+# vars file for ansible-rundeck-jobs
\ No newline at end of file