如何用Ansible Playbook实现按目标服务器名复制文件并部署RPM
Got it, let's turn that shell script logic into a robust, idempotent Ansible Playbook. This solution will handle file discovery, server name extraction, file transfer, and remote RPM installation—all in Ansible's declarative style.
Full Playbook Setup
We'll split this into a main playbook and a subtask file for clarity, but you can also combine them if you prefer.
1. Main Playbook (deploy_rpm.yml)
This playbook starts by discovering the server-specific files on your control node, then loops through each to trigger deployment tasks.
--- - name: Discover server files and trigger deployment hosts: localhost gather_facts: false tasks: - name: Find all server-specific files in /var/tmp ansible.builtin.find: path: /var/tmp patterns: "*.*" # Matches [ServerName].[extension] filename format file_type: file register: server_files - name: Process each server file to deploy and install RPM ansible.builtin.include_tasks: deploy_to_server.yml loop: "{{ server_files.files }}" loop_control: loop_var: server_file
2. Subtask File (deploy_to_server.yml)
This file handles the per-server logic: extracting the server name, transferring files, and installing the RPM.
--- - name: Extract target server name from filename ansible.builtin.set_fact: target_server: "{{ server_file.path | basename | regex_replace('\\..*$', '') }}" delegate_to: localhost - name: Deploy files and install RPM on target server hosts: "{{ target_server }}" gather_facts: false become: yes # Required if yum needs sudo privileges tasks: - name: Copy server-specific file to /var/tmp on target ansible.builtin.copy: src: "{{ server_file.path }}" dest: /var/tmp/ mode: '0644' - name: Copy pkg.rpm to /var/tmp on target ansible.builtin.copy: src: /var/tmp/pkg.rpm # Adjust this path if your pkg.rpm is elsewhere dest: /var/tmp/pkg.rpm mode: '0644' - name: Install RPM package locally via yum ansible.builtin.yum: name: /var/tmp/pkg.rpm state: present # Uncomment below if your RPM isn't GPG-signed # disable_gpg_check: true
Key Details & Adjustments
Inventory Requirements: Ensure the extracted
target_servernames match host entries in your Ansible inventory (or are resolvable via DNS). If your servers aren't pre-configured in inventory, add anadd_hosttask before the deployment play to temporarily register them:- name: Add target server to temporary inventory ansible.builtin.add_host: name: "{{ target_server }}" ansible_user: your_ssh_username # Replace with your SSH user # Add ansible_ssh_private_key_file if using key-based auth # ansible_ssh_private_key_file: /path/to/your/private/key delegate_to: localhostSudo Privileges: The
become: yesline assumes your SSH user has sudo access to run yum. Adjust or remove this if your user has direct root access.RPM Source Path: If
pkg.rpmisn't located in/var/tmpon your control node, update thesrcpath in the secondcopytask.Installing the Server-Specific File: If the server-specific file (e.g.,
server1.rpm) is the one you need to install instead ofpkg.rpm, modify the yum task'snameparameter:name: "/var/tmp/{{ server_file.path | basename }}"Idempotency: Ansible will skip file copies if the files already exist and match the source, and skip installation if the RPM is already installed—this makes the playbook safe to run multiple times.
How to Run
Execute the main playbook with:
ansible-playbook deploy_rpm.yml
If you need to provide SSH credentials, use:
ansible-playbook deploy_rpm.yml --ask-pass --ask-become-pass
内容的提问来源于stack exchange,提问作者Websy

