1. include
利用include可以将一个Playbook根据具体的操作拆分成多个文件,更容易组织和管理Playbook。
- 定义一个安装nginx的yaml文件
# cat install_nginx.yml
- name: install nginx
package:
name: nginx
state: present
- name: ensure nginx enabled and started
systemd:
name: nginx
state: started
enabled: yes
- name: config nginx
copy:
src: /etc/nginx/nginx.conf # 可以维护一个自己的配置,这里只是为了测试,用template配置更灵活
dest: /etc/nginx/nginx.conf
notify: restart nginx
- 定义一个重启nginx的yaml文件
# cat restart_nginx.yml
- name: restart nginx
systemd:
name: nginx
state: restarted
- 定义一个入口文件,可在这个文件执行
ansible-playbook -i hosts nginx.yml
# cat nginx.yml # 相当于一个入口文件
---
- hosts: proxy
tasks:
- include: install_nginx.yml
handlers:
- include: restart_nginx.yml
2. roles
这个是ansible提供的又一个非常灵活且强大的功能,就是强!可以基于ansible-galaxy初始化配置目录,也可以基于ansible galaxy网站找一些比较不错的roles下载下来使用。
- 初始化roles目录结构
# ansible-galaxy init deploy
- Role deploy was created successfully
# tree deploy
deploy
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
- 查看其它命令
# ansible-galaxy role -h
usage: ansible-galaxy role [-h] ROLE_ACTION ...
positional arguments:
ROLE_ACTION
init Initialize new role with the base structure of a role.
remove Delete roles from roles_path.
delete Removes the role from Galaxy. It does not remove or alter the
actual GitHub repository.
list Show the name and version of each role installed in the
roles_path.
search Search the Galaxy database by tags, platforms, author and
multiple keywords.
import Import a role
setup Manage the integration between Galaxy and the given source.
login Login to api.github.com server in order to use ansible-galaxy
role sub command such as 'import', 'delete', 'publish', and
'setup'
info View more details about a specific role.
install Install role(s) from file(s), URL(s) or Ansible Galaxy