You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible Playbook实现按目标服务器名复制文件并部署RPM

Ansible Playbook Implementation for Your Deployment Workflow

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_server names match host entries in your Ansible inventory (or are resolvable via DNS). If your servers aren't pre-configured in inventory, add an add_host task 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: localhost
    
  • Sudo Privileges: The become: yes line 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.rpm isn't located in /var/tmp on your control node, update the src path in the second copy task.

  • Installing the Server-Specific File: If the server-specific file (e.g., server1.rpm) is the one you need to install instead of pkg.rpm, modify the yum task's name parameter:

    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:06:04