Ansible playbook安装nginx(Linux安装nginx详细步骤)

以下是不念写的一个简单的示例Playbook,主要用于在 Ubuntu 和 CentOS 系统上安装 Nginx。

步骤如下:

创建一个名为install_nginx.yml的 YAML 文件,然后可以根据需求和使用的操作系统进行调整。

--- - name: Install and configure Nginx on Ubuntu and CentOS hosts: all become: yes tasks: - name: Install Nginx on Ubuntu block: - name: Add APT repository for Nginx ansible.builtin.apt_repository: repo: "ppa:nginx/stable" update_cache: yes when: ansible_facts['os_family'] == 'Debian' - name: Install Nginx on Ubuntu ansible.builtin.apt: name: nginx state: present when: ansible_facts['os_family'] == 'Debian' when: ansible_facts['os_family'] == 'Debian' - name: Install Nginx on CentOS block: - name: Install EPEL repository ansible.builtin.yum: name: epel-release state: present when: ansible_facts['os_family'] == 'RedHat' - name: Install Nginx on CentOS ansible.builtin.yum: name: nginx state: present when: ansible_facts['os_family'] == 'RedHat' when: ansible_facts['os_family'] == 'RedHat' - name: Start and enable Nginx service ansible.builtin.systemd: name: nginx state: started enabled: yes

在这个示例 Playbook 中,我们首先检查目标系统的操作系统类型,然后根据系统类型选择执行相应的任务。

对于 Ubuntu,我们添加 Nginx 的 APT 源,然后使用apt模块安装 Nginx。

对于 CentOS,我们安装 EPEL 源,然后使用yum模块安装 Nginx。

最后,我们使用systemd模块启用并启动 Nginx 服务。

要运行此 Playbook,请将目标主机添加到你的 inventory 文件中,并执行以下命令:

ansible-playbook -i inventory.ini install_nginx.yml

请注意,将inventory.ini替换为你的实际 inventory 文件名。

阅读剩余
THE END