Ansible的常用模块
Ansible 提供了众多模块来实现自动化任务,如管理文件、包、用户、服务等。以下是一些常用的模块和一个简单的 playbook
示例。
常用的 Ansible 模块
ping
:用于测试节点连通性。- name: Ping all hosts ansible.builtin.ping:
yum
/apt
:用于安装、更新或删除软件包。- name: Install Apache ansible.builtin.yum: name: httpd state: present
service
:管理服务的启动、停止、重启等。- name: Start Apache service ansible.builtin.service: name: httpd state: started
copy
:用于将文件从控制节点复制到目标节点。- name: Copy a file to the remote server ansible.builtin.copy: src: /local/path/to/file dest: /remote/path/to/file mode: '0644'
file
:管理文件属性,包括文件、目录的创建、删除及权限设置。- name: Ensure /opt/mydir exists ansible.builtin.file: path: /opt/mydir state: directory mode: '0755'
user
:管理用户账号。- name: Add a user ansible.builtin.user: name: deployuser state: present groups: wheel
cron
:用于管理计划任务。- name: Schedule a cron job ansible.builtin.cron: name: "Run backup" minute: "0" hour: "2" job: "/usr/local/bin/backup.sh"
command
:执行特定命令(不通过 shell 环境)。- name: Run a command ansible.builtin.command: /usr/bin/uptime
shell
:执行 shell 命令,适用于需要管道、重定向等 shell 功能的命令。- name: Run a shell script ansible.builtin.shell: "echo hello world > /tmp/hello.txt"
git
:用于管理 Git 仓库的克隆和更新。- name: Clone a Git repository ansible.builtin.git: repo: 'https://github.com/example/repo.git' dest: /opt/repo
简单的 Ansible Playbook 示例
下面是一个简单的 playbook
示例,展示如何在目标服务器上安装 Apache、创建目录、部署配置文件、启动服务等。
Playbook 文件:setup_web_server.yml
---
- name: Setup a basic web server
hosts: webservers # 目标主机组
become: yes # 提升到 root 权限
tasks:
- name: Install Apache
ansible.builtin.yum:
name: httpd
state: present
- name: Start and enable Apache service
ansible.builtin.service:
name: httpd
state: started
enabled: true
- name: Create a directory for website content
ansible.builtin.file:
path: /var/www/html/mywebsite
state: directory
mode: '0755'
- name: Deploy a sample index.html file
ansible.builtin.copy:
content: "<h1>Welcome to My Website</h1>"
dest: /var/www/html/mywebsite/index.html
mode: '0644'
- name: Open HTTP port in firewall
ansible.builtin.firewalld:
port: 80/tcp
permanent: yes
state: enabled
执行 Playbook
在终端中执行以下命令来运行 Playbook:
ansible-playbook -i inventory setup_web_server.yml
-i inventory
:指定inventory
文件。setup_web_server.yml
:Playbook 文件名。
Ansible如何安装一个nginx ,说下yml
要使用 Ansible 安装 Nginx,可以编写一个简单的 playbook
来自动化安装过程。在这个示例中,我们将安装 Nginx、启动服务,并确保服务在系统启动时自动启动。
Ansible Playbook 示例:安装 Nginx
文件名:install_nginx.yml
---
- name: Install and configure Nginx web server
hosts: webservers # 目标主机组名
become: yes # 提升为 root 权限
tasks:
- name: Install Nginx
ansible.builtin.yum: # 使用 yum 模块(适用于 CentOS/RHEL)
name: nginx
state: present
when: ansible_os_family == "RedHat" # 条件:适用于 RedHat 系列系统
- name: Install Nginx on Debian/Ubuntu
ansible.builtin.apt: # 使用 apt 模块(适用于 Debian/Ubuntu)
name: nginx
state: present
update_cache: yes # 更新 apt 缓存
when: ansible_os_family == "Debian" # 条件:适用于 Debian 系列系统
- name: Start and enable Nginx service
ansible.builtin.service:
name: nginx
state: started # 启动服务
enabled: yes # 设置为开机启动
Playbook 详细说明
hosts: webservers
:指定运行 playbook 的目标主机组(需在 inventory 文件中定义)。become: yes
:使用 sudo 提升权限以安装软件。tasks
:- 安装 Nginx:根据操作系统的不同,使用不同模块安装 Nginx。
- 如果是 RedHat 系列(如 CentOS),使用
yum
模块。 - 如果是 Debian 系列(如 Ubuntu),使用
apt
模块,并在安装前更新 apt 缓存。
- 如果是 RedHat 系列(如 CentOS),使用
- 启动和启用 Nginx 服务:确保 Nginx 服务已启动并设置为开机启动。
- 安装 Nginx:根据操作系统的不同,使用不同模块安装 Nginx。
执行 Playbook
在终端中运行以下命令来执行 playbook:
ansible-playbook -i inventory install_nginx.yml
ansible中的判断是什么关键字(when)
作者:严锋 创建时间:2024-10-12 08:48
最后编辑:严锋 更新时间:2024-11-06 17:44
最后编辑:严锋 更新时间:2024-11-06 17:44