在playbook中有一些核心的指令
hosts:指明命令运行在哪个node之上
remote_user:在远端的node之上以什么用户的身份运行命令
var:给模板传递变量值
tasks:指明需要执行的命令
template:指明模板文件
handler:指明句柄所在文件目录名
Roles:指明需要运行的角色名
(一)检查playbook是否正确
ansible-playbook --
syntax-check first.yml
(二)预执行playbook
ansible-playbook
--check first.yml
(三)查看执行命令的node节点的IP地址或主机名
ansible-playbook --
list-hosts first.yml
first.yml
- hosts: webservers
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=latest
- name: distribute config file
copy: src=config/httpd.conf dest=/etc/httpd/conf/httpd.conf
tags: redistibute
notify: restart httpd
- name: start httpd
service: name=httpd state=started enabled=yes
- name: check httpd started
shell: netstat -lntup | grep 80
handlers:
- name: restart httpd
service: name=httpd state=restarted enabled=yes
(四)使用vars定义变量
-
hosts: dbservers
remote_user: root
vars:
-
username: home
-
groupname: uplooking
tasks:
-
name: add group
group: name={{ groupname }}
-
name: add user
user: name={{ username }} state=present