--- - name: XXXigCC Pool URL 更新脚本 hosts: all become: yes vars: # 必填参数 new_url: "" # 可选参数 old_url: "" # 配置文件路径 config_file: "/etc/xxxigcc/config.json" log_dir: "/var/log/xxxigcc" service_name: "xxxigcc-daemon.service" tasks: - name: 检查 new_url 参数是否提供 ansible.builtin.fail: msg: "参数 new_url 是必填项,请使用 -e new_url=your_url 提供" when: new_url == "" - name: 确保 jq 工具已安装 ansible.builtin.package: name: jq state: present - name: 检查配置文件是否存在 ansible.builtin.stat: path: "{{ config_file }}" register: config_file_stat - name: 验证配置文件存在 ansible.builtin.fail: msg: "配置文件 {{ config_file }} 不存在" when: not config_file_stat.stat.exists - name: 备份配置文件 ansible.builtin.copy: src: "{{ config_file }}" dest: "{{ config_file }}.backup.{{ ansible_date_time.epoch }}" remote_src: yes mode: preserve - name: 获取当前的 pool URL ansible.builtin.shell: | jq -r '.pools[0].url' {{ config_file }} register: current_url changed_when: false - name: 显示当前 pool URL ansible.builtin.debug: msg: "当前 pool URL: {{ current_url.stdout }}" - name: 检查是否需要更新(提供了 old_url 参数时) ansible.builtin.set_fact: should_update: "{{ current_url.stdout == old_url }}" when: old_url != "" - name: 检查是否需要更新(未提供 old_url 参数时) ansible.builtin.set_fact: should_update: true when: old_url == "" - name: 显示更新决策 ansible.builtin.debug: msg: > {% if old_url != "" %} old_url 参数已提供: {{ old_url }},当前值: {{ current_url.stdout }}, {% if should_update %}将执行更新{% else %}跳过更新(当前值与 old_url 不匹配){% endif %} {% else %} 未提供 old_url 参数,将无条件更新 {% endif %} - name: 更新 pool URL ansible.builtin.shell: | jq '.pools[0].url = "{{ new_url }}"' {{ config_file }} > {{ config_file }}.tmp && \ mv {{ config_file }}.tmp {{ config_file }} when: should_update | bool register: update_result - name: 显示更新后的 pool URL ansible.builtin.shell: | jq -r '.pools[0].url' {{ config_file }} register: updated_url changed_when: false when: should_update | bool - name: 确认更新结果 ansible.builtin.debug: msg: "pool URL 已更新为: {{ updated_url.stdout }}" when: should_update | bool - name: 检查日志目录是否存在 ansible.builtin.stat: path: "{{ log_dir }}" register: log_dir_stat - name: 删除日志目录下的所有文件 ansible.builtin.shell: | rm -rf {{ log_dir }}/* args: warn: false when: log_dir_stat.stat.exists and log_dir_stat.stat.isdir register: log_cleanup - name: 显示日志清理结果 ansible.builtin.debug: msg: "日志目录 {{ log_dir }} 已清理" when: log_dir_stat.stat.exists and log_dir_stat.isdir - name: 重启 xxxigcc-daemon 服务 ansible.builtin.systemd: name: "{{ service_name }}" state: restarted when: should_update | bool register: service_restart - name: 等待服务重启完成 ansible.builtin.wait_for: timeout: 5 when: should_update | bool - name: 验证服务状态 ansible.builtin.systemd: name: "{{ service_name }}" state: started register: service_status when: should_update | bool - name: 显示操作总结 ansible.builtin.debug: msg: | 操作完成总结: - 配置文件备份: {{ config_file }}.backup.{{ ansible_date_time.epoch }} {% if should_update | bool %} - Pool URL 已从 '{{ current_url.stdout }}' 更新为 '{{ new_url }}' - 日志目录已清理: {{ log_dir }} - 服务已重启: {{ service_name }} {% else %} - Pool URL 未更新(条件不满足) {% endif %}