以下是一个使用Ansible Playbook在目标主机上安装并添加systemd服务的示例:

---
- name: Install and enable systemd service
  hosts: target_hosts
  become: true
  vars:
    service_name: myservice
    service_file: myservice.service

  tasks:
    - name: Copy service file
      copy:
        src: /path/to/{{ service_file }}
        dest: /etc/systemd/system/{{ service_file }}
        owner: root
        group: root
        mode: '0644'

    - name: Reload systemd
      systemd:
        daemon_reload: yes

    - name: Enable service
      systemd:
        name: "{{ service_name }}"
        enabled: yes
        state: started

在上面的示例中,将target_hosts替换为目标主机的名称或组。become: true表示使用sudo权限执行任务。

vars部分定义了服务的名称和服务文件的名称。

任务部分包括以下步骤:

  1. 使用copy模块将服务文件从控制节点复制到目标主机的/etc/systemd/system/目录下。
  2. 使用systemd模块重新加载systemd服务配置。
  3. 使用systemd模块启用并启动服务。

在运行Playbook时,将会在目标主机上安装并添加systemd服务,并启用该服务

使用ansible playbook安装添加systemd服务并启用

原文地址: http://www.cveoy.top/t/topic/hNGm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录