在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

相关文章:

  • 2023-02-20
  • 2022-12-23
  • 2022-01-07
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
  • 2021-05-19
  • 2021-11-08
  • 2022-01-07
  • 2022-01-07
相关资源
相似解决方案