CentOS 下 FetchLinux 的自动化运维实践
一 概念澄清与定位
二 标准化落地流程
三 示例 Playbook 与任务片段
---
- hosts: webservers
become: yes
vars:
remote_user: deploy
src_base: /opt/artifacts
dst_base: /var/www/releases
tasks:
- name: Ensure destination exists
file:
path: "{{ dst_base }}"
state: directory
mode: '0755'
- name: Fetch files via SFTP/FTP (delegated to fetchlinux CLI)
command: >
fetchlinux {{ remote_user }}@{{ inventory_hostname }}:{{ src_base }}/app-{{ version }}.tar.gz
{{ dst_base }}/app-{{ version }}.tar.gz
register: fetch_result
changed_when: fetch_result.rc == 0
- name: Verify checksum
shell: |
set -o pipefail
sha256sum {{ dst_base }}/app-{{ version }}.tar.gz | cut -d' ' -f1
register: sha_out
- name: Fail if checksum mismatch
fail:
msg: "Checksum mismatch for {{ dst_base }}/app-{{ version }}.tar.gz"
when: sha_out.stdout != expected_sha256
- name: Extract and restart service
unarchive:
src: "{{ dst_base }}/app-{{ version }}.tar.gz"
dest: "{{ dst_base }}"
remote_src: yes
notify: restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
# 1) 下载镜像并校验
fetchlinux download http://mirror.example/fedora.iso /opt/iso/fedora.iso
echo "<expected_sha256> /opt/iso/fedora.iso" | sha256sum --check
# 2) 增量同步到备份存储
rsync -aAX --partial /opt/iso/ backup@nas.example:/backup/iso/
# 3) 在目标主机上分发(示例)
fetchlinux user@host:/opt/iso/fedora.iso /opt/iso/fedora.iso
- hosts: all
become: yes
tasks:
- name: Ensure .ssh exists
file:
path: /home/{{ ansible_user }}/.ssh
state: directory
mode: '0700'
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- name: Deploy admin public key
authorized_key:
user: "{{ ansible_user }}"
key: "{{ lookup('file', 'files/id_rsa_admin.pub') }}"
state: present
- name: Disable password authentication in sshd
lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PasswordAuthentication'
line: 'PasswordAuthentication no'
state: present
notify: restart sshd
handlers:
- name: restart sshd
service:
name: sshd
state: restarted
以上片段展示了“传输类(FetchLinux CLI)+ 编排类(Ansible)+ 校验与回滚”的组合用法;具体命令与参数请以你所用 FetchLinux 版本的文档为准。
四 最佳实践与风险控制