下面给你一个从零到可用的思路,用 Ansible 部署 Web 服务器(以 Nginx 为例),适合初学者也能直接落地。
假设环境:
目标:
# Ubuntu / Debian
sudo apt update
sudo apt install ansible -y
# CentOS
sudo yum install epel-release -y
sudo yum install ansible -y
ssh-keygen
ssh-copy-id user@web-server-ip
inventory.ini
[web]
192.168.1.10 ansible_user=ubuntu
测试连通性:
ansible web -i inventory.ini -m ping
deploy_web.yml
---
- name: Deploy Nginx Web Server
hosts: web
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: true
- name: Deploy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
mode: '0644'
files/index.html
<!DOCTYPE html>
<html>
<head>
<title>Ansible Web</title>
</head>
<body>
<h1>Hello from Ansible!</h1>
</body>
</html>
ansible-playbook -i inventory.ini deploy_web.yml
浏览器访问:
http://web-server-ip
vars:
web_root: /var/www/html
[web]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
ansible-galaxy init nginx
目录结构:
roles/
└── nginx/
├── tasks/
├── templates/
└── handlers/
become: truesystemctl status nginx-v 或 -vvv如果你愿意,我可以:
你现在的系统是 Ubuntu 还是 CentOS?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。