- hosts: all #定义执行脚本的主机组
remote_user: liangkai #定义远程操作的用户
become: yes # 调用sudo
become_method: sudo
become_user: root
tasks: # 开始执行任务
- name: create hello-file # 类似于描述
file: path=/root/hello-1 state=touch mode=0755 #调用具体模块
ignore_errors: True #忽略本条命令的报错
==============================================================
- hosts: all
remote_user: root #远程用户可以改变
tasks:
- name: create hello-file
file: path=/root/hello state=touch mode=555
- name: create hello-file-2
file: path=/root/hello-2 state=touch mode=555
- name: create hello-file
file: path=/root/hello-3 state=touch mode=555
{ hosts和tasks是一组,一组里只能存在一个tasks,一组内有多个tasks,
仅执行最后一个tasks,或者参考第二组的一个tasks调用多个模块}
检测Playbook正确性
[root@localhost ~]# ansible-playbook -C hello.yml
-C 检测
正式运行
[root@localhost ~]# ansible-playbook hello.yml
忽略Playbook中的错
- hosts: all
remote_user: liangkai
tasks:
- name: install nginx
yum: name=nginx state=latest
ignore_errors: True #忽略本条命令的报错
ignore_errors: True
:命令报错后,结果返回为真,脚本继续往下执行
本篇以简单的playbook example,来帮助大家更好的理解,其实好好感受会发现并不难。
通过编写playbook来将重复的工作简单化也是很有成就感的哦。
学习playbook之前最好先理解一下ansible的模块语法,会更有助于理解playbook
后面我会再更新一篇关于ansible常用模块语法的文章,觉得对你有帮助的话,记得点个赞哦