修改Rundeck项目下所有作业的选项strict值。

huangapple go评论64阅读模式
英文:

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: &quot;Replacing Target Host&quot;
  hosts: localhost
  any_errors_fatal: true
  become: yes
  become_user: root
  vars_prompt:
    - name: &quot;rundeck_project&quot;
      prompt: &quot;Rundeck project&quot;
      private: no
    - name: &quot;old_target&quot;
      prompt: &quot;Old target name&quot;
      private: no
    - name: &quot;new_target&quot;
      prompt: &quot;New target name&quot;
      private: no

  tasks:

  - block:

    - name: &quot;Listing all jobs from a Rundeck project&quot;
      shell: &quot;rd jobs list --project {{ rundeck_project }} | awk &#39;{print $1}&#39; | tail -n +2&quot;
      register: invar_jobs_id

    - name: &quot;Store jobs in a yaml file&quot;
      shell: &quot;rd jobs list --project {{ rundeck_project }} -i {{ item }} -f /path/to/the/directory/{{ item }}.yml -F yaml&quot;
      loop: &quot;{{ invar_jobs_id.stdout_lines }}&quot;
      register: invar_jobs_file

    - name: &quot;Replace target&quot;
      shell: &quot;sed -i &#39;s/{{ old_target }}/{{ new_target }}/g&#39; /path/to/the/directory/{{ item.stdout_lines[0] | regex_search(&#39;(?&lt;=/path/to/the/directory/).*\\.yml&#39;) }}&quot;
      loop: &quot;{{ invar_jobs_file.results }}&quot;

    - name: &quot;Import jobs from yaml files to Rundeck&quot;
      shell: &quot;rd jobs load -p {{ rundeck_project }}  -f /path/to/the/directory/{{ item.stdout_lines[0] | regex_search(&#39;(?&lt;=/path/to/the/directory/).*\\.yml&#39;) }} -F yaml&quot;
      loop: &quot;{{ invar_jobs_file.results }}&quot;

    always:

    - name: &quot;Removing files&quot;
      shell: &quot;rm -rf /path/to/the/directory/*&quot;

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.

huangapple
  • 本文由 发表于 2023年5月18日 06:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定