美文网首页
playbook语法

playbook语法

作者: 泡面_b7f5 | 来源:发表于2018-05-31 17:13 被阅读0次

    playbook使用yaml语法yam语法可以通过http://www.yaml.org/spec/1.2/spec.html#Syntax 了解到

    讲解一个简单的例子来认识playbook代码--安装nginx

    nginx.yml

    旧版语法

    ---
    - hosts: all
      tasks:
        - name: Install nginx
          yum: name=nginx state=present
        - name: template nginx.conf
          template: src=./nginx.conf.js dest=/etc/nginx/nginx.conf owner=root group=root mode=0644 validate='nginx -t -c %s'
          notify:
            - Restart Nginx Service
      handlers:
        - name: Restart Nginx Service
          service: name=nginx state=restarted
    
    • 第一行表示该文件是YAML文件,非必须
    • 第二行定义了改playbook针对的目标主机,all表示针对所有主机,这个- 采纳数支特ansible Ad-Hoc模式的所有参数也就是可以定义组
    • 第三行定义了改playbook所有的tasks集合,比如下面我们定义的3个tasks
    • 第四行定义一个tasks的名称,非必须,建议根据tasks实际任务命名
    • 第五行定义了一个状态的action,这里使用的是yum模块安装NGINX软件包
    • 第六行到第九行使用template模板去管理/etc/nginx/nginx.conf文件,owner group定义该文件的属主以及属组,使用validate参数指定文件生产后使用nginx -t -c %s命令去做nginx文件语法验证(%s意思是前面的路径,即/etc/nginx/nginx.conf,这条命令可能会报错,报错就删了),notify是触发handlers,如果同步后,文件的MDS值有变化会触发ReStart nginx serveice这个hangler
    • 第十行到第十二行是定义一个handler状态让nginx服务重启,handler的名称是restart nginx service

    新版语法

    ---
    - hosts: all
      tasks:
        - name: Install nginx
          yum: 
            name: nginx 
            state: present
        - name: template nginx.conf
          template: 
            src: ./nginx.conf 
            dest: /etc/nginx/nginx.conf 
            owner: root 
            group: root 
            mode: '0644'
          notify:
            - Restart Nginx Service
      handlers:
        - name: Restart Nginx Service
          service: 
            name: nginx 
            state: restarted
    

    hosts

    [nginx]
    192.168.1.1
    

    检查语法

    ansible-playbook --syntax-check nginx.yaml

    列出执行任务

    ansible-playbook --list-task nginx.yaml

    查看运行的主机

    ansible-playbook --list-hosts nginx.yaml

    执行

    ansible-playbook nginx.yaml

    相关文章

      网友评论

          本文标题:playbook语法

          本文链接:https://www.haomeiwen.com/subject/vdiqsftx.html