66 lines
1.7 KiB
Markdown
66 lines
1.7 KiB
Markdown
```
|
||
---
|
||
- hosts: webservers
|
||
vars:
|
||
http_port: 80
|
||
max_clients: 200
|
||
remote_user: root
|
||
tasks:
|
||
- name: ensure apache is at the latest version
|
||
yum: pkg=httpd state=latest
|
||
- name: write the apache config file
|
||
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
|
||
notify:
|
||
- restart apache
|
||
- name: ensure apache is running
|
||
service: name=httpd state=started
|
||
handlers:
|
||
- name: restart apache
|
||
service: name=httpd state=restarted
|
||
```
|
||
#### 主机与用户
|
||
你可以为 playbook 中的每一个 play,个别地选择操作的目标机器是哪些,以哪个用户身份去完成要执行的步骤(called tasks)
|
||
hosts 行的内容是一个或多个组或主机的 patterns,以逗号为分隔符,remote_user 就是账户名
|
||
```
|
||
---
|
||
- hosts: webservers
|
||
remote_user: root
|
||
```
|
||
在每一个 task 中,可以定义自己的远程用户
|
||
```
|
||
---
|
||
- hosts: webservers
|
||
remote_user: root
|
||
tasks:
|
||
- name: test connection
|
||
ping:
|
||
remote_user: yourname
|
||
```
|
||
支持从 sudo 执行命令
|
||
```
|
||
---
|
||
- hosts: webservers
|
||
remote_user: yourname
|
||
sudo: yes
|
||
```
|
||
可以仅在一个 task 中,使用 sudo 执行命令,而不是在整个 play 中使用 sudo
|
||
```
|
||
---
|
||
- hosts: webservers
|
||
remote_user: yourname
|
||
tasks:
|
||
- service: name=nginx state=started
|
||
sudo: yes
|
||
```
|
||
可以登陆后,sudo 到不同的用户身份,而不是使用 root
|
||
```
|
||
---
|
||
- hosts: webservers
|
||
remote_user: yourname
|
||
sudo: yes
|
||
sudo_user: postgres
|
||
```
|
||
需要在使用 sudo 时指定密码,可在运行 ansible-playbook 命令时加上选项 --ask-sudo-pass (-K). 如果使用 sudo 时,playbook 疑似被挂起,可能是在 sudo prompt 处被卡住,这时可执行 Control-C 杀死卡住的任务,再重新运行一次
|
||
|
||
#### Tasks 列表
|
||
|