英文:
Changing option strict value on all the jobs under a Rundeck project
问题
I've been using rundeck to run ansible playbooks on several remote hosts. My current structure is one rundeck project per host. I've been importing the project's configuration and jobs from one project to another so I don't have to set everything manually for each one.
I'm looking for a way to bulk edit a variable/option on all the project's jobs without having to manually edit each one. For instance, I have the option "target" which has a strict value that matches the remote host that's supposed to run the playbook. Since I'm importing all the jobs from a previous project to a new one, I need to change the strict value of the option "target" on the new project.
Thank you in advance for your assistance.
英文:
I've been using rundeck to run ansible playbooks on several remote hosts. My current structure is one rundeck project per host. I've been importing the project's configuration and jobs from one project to another so I don't have to set everything manually for each one.
I'm looking for a way to bulk edit a variable/option on al the project's jobs without having to manually edit each one. For instance, I have the option "target" which has a strict value that matches the remote host that's supposed to run the playbook. Since I'm importing all the jobs from a previous project to a new one, I need to change the strict value of the option "target" on the new project.
Thank you in advance for your assistance.
答案1
得分: 1
感谢您的帮助!我通过遵循@MegaDrive68k的建议成功完成了我所需的操作。我创建了一个Ansible playbook来使用RD-CLI修改所有作业。它看起来像这样:
---
- name: "替换目标主机"
hosts: localhost
any_errors_fatal: true
become: yes
become_user: root
vars_prompt:
- name: "rundeck_project"
prompt: "Rundeck项目"
private: no
- name: "old_target"
prompt: "旧目标名称"
private: no
- name: "new_target"
prompt: "新目标名称"
private: no
tasks:
- block:
- name: "列出来自Rundeck项目的所有作业"
shell: "rd jobs list --project {{ rundeck_project }} | awk '{print $1}' | tail -n +2"
register: invar_jobs_id
- name: "将作业存储在yaml文件中"
shell: "rd jobs list --project {{ rundeck_project }} -i {{ item }} -f /path/to/the/directory/{{ item }}.yml -F yaml"
loop: "{{ invar_jobs_id.stdout_lines }}"
register: invar_jobs_file
- name: "替换目标"
shell: "sed -i 's/{{ old_target }}/{{ new_target }}/g' /path/to/the/directory/{{ item.stdout_lines[0] | regex_search('(?<=/path/to/the/directory/).*\\.yml') }}"
loop: "{{ invar_jobs_file.results }}"
- name: "从yaml文件导入作业到Rundeck"
shell: "rd jobs load -p {{ rundeck_project }} -f /path/to/the/directory/{{ item.stdout_lines[0] | regex_search('(?<=/path/to/the/directory/).*\\.yml') }} -F yaml"
loop: "{{ invar_jobs_file.results }}"
always:
- name: "删除文件"
shell: "rm -rf /path/to/the/directory/*"
请注意,为了让Rundeck命令能够访问和修改您的Rundeck项目的信息,您必须定义环境变量RD_URL和RD_TOKEN。
英文:
Thank you for your help! I was able to accomplish what I was looking for by following the suggestion made by @MegaDrive68k. I created an Ansible playbook to use the RD-CLI to modify all the jobs. It looks like this:
---
- name: "Replacing Target Host"
hosts: localhost
any_errors_fatal: true
become: yes
become_user: root
vars_prompt:
- name: "rundeck_project"
prompt: "Rundeck project"
private: no
- name: "old_target"
prompt: "Old target name"
private: no
- name: "new_target"
prompt: "New target name"
private: no
tasks:
- block:
- name: "Listing all jobs from a Rundeck project"
shell: "rd jobs list --project {{ rundeck_project }} | awk '{print $1}' | tail -n +2"
register: invar_jobs_id
- name: "Store jobs in a yaml file"
shell: "rd jobs list --project {{ rundeck_project }} -i {{ item }} -f /path/to/the/directory/{{ item }}.yml -F yaml"
loop: "{{ invar_jobs_id.stdout_lines }}"
register: invar_jobs_file
- name: "Replace target"
shell: "sed -i 's/{{ old_target }}/{{ new_target }}/g' /path/to/the/directory/{{ item.stdout_lines[0] | regex_search('(?<=/path/to/the/directory/).*\\.yml') }}"
loop: "{{ invar_jobs_file.results }}"
- name: "Import jobs from yaml files to Rundeck"
shell: "rd jobs load -p {{ rundeck_project }} -f /path/to/the/directory/{{ item.stdout_lines[0] | regex_search('(?<=/path/to/the/directory/).*\\.yml') }} -F yaml"
loop: "{{ invar_jobs_file.results }}"
always:
- name: "Removing files"
shell: "rm -rf /path/to/the/directory/*"
Note you must define the env variables RD_URL and RD_TOKEN in order for the Rundeck commands to access and modify the information of your Rundeck projects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论