试试用ansible安装nginx
概括
我尝试使用 Ansible 安装了 Nginx。
环境
ansible 2.4.0.0
CentOS 6.9 发行版
Ansible的配置
我修改了ansible.cfg文件的某些设置。
inventory = /etc/ansible/hosts
remote_user = user_name
/etc/ansible/hosts文件定义了install先服务器的配置。
[webserver]
web1
创建一个推荐目录
ansible-galaxy init nginx
目录结构
.
├── nginx.yml
└── roles
├── common
└── nginx
├── README.md
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
主要的nginx.yml文件
- name: nginx
become: yes
hosts: web1
roles:
- { role: nginx }
操作 Nginx 的任务
有没有更好的方法来改变以”–extra-vars ‘op=xxxx'”作为参数执行的任务?
---
# tasks file for nginx
- name: Install nginx
yum: name=nginx
when: op == "install"
- name: Start nginx
service: name=nginx enabled=yes state=started
when: op == "start"
- name: Stop nginx
service: name=nginx state=stopped
when: op == "stop"
- name: Restart nginx
service: name=nginx state=restarted
when: op == "restart"
- name: Reload nginx
service: name=nginx state=reloaded
when: op == "reload"
执行ansible-playbook
ansible-playbook nginx.yml --extra-vars "op=start"
服务器端的sudo已设置为无需密码。