认识ansible-Playbook

- 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常用模块语法的文章,觉得对你有帮助的话,记得点个赞哦

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容