首页 > 其他 > 详细

playbook 实例

时间:2019-02-22 17:12:58      阅读:176      评论:0      收藏:0      [点我收藏+]

变量使用及错误处理

---
- hosts: db
  remote_user: root
  vars:
    user: dd
    pwd: aa
  tasks:
    - name: add user
      user:
        name: "{{ user }}"
        password: "{{ ‘{{pwd}}‘ | password_hash(‘sha512‘)}}"
    - name: set account valid date
      shell: chage -d 0 "{{ user }}"
ignore_errors: true # 忽略错误

 

安装apache

---
- hosts: web
  remote_user: root
  tasks:
    - name: install the latest version of Apache
      yum:                                                       
        name: httpd
        state: latest
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: ^Listen 
        insertafter: ^#Listen 
        line: Listen 8080
    - lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: ^#ServerName
        line: ServerName localhost
    - copy:
        src: index.html
        dest: /var/www/html/index.html
        owner: apache
        group: apache
        mode: 0644
    - service:
        name: httpd
        state: started
        enabled: yes

 

when 条件判断

---
- hosts: web
  remote_user: root
  tasks:
    - shell: uptime | awk {printf("%.2f", $(NF-2))}
      register: result                           
    - service:
        name: httpd
        state: stopped
      when: result.stdout|float > 0.7

 

handlers 触发

---
- hosts: cache
  remote_user: root
  tasks:
    - copy:
        src: /root/httpd.conf
        dest:  /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 0644
      notify:
        - restart httpd
  handlers:
     - name: restart httpd
       service: name=httpd state=restarted

 

withe_item 循环

---
- hosts: cache
  remote_user: root
  tasks:
    - user:
        name: "{{item.name}}"
        group: "{{item.group}}"
        password: "{{item.pwd|password_hash(‘sha512‘)}}"
      with_items:
        - 
          name: a1
          pwd: aa
          group: users
        - 
          name: a2
          pwd: bb
          group: wheel
        - 
          name: a3
          pwd: cc
          group: root

 

playbook 实例

原文:https://www.cnblogs.com/ray-mmss/p/10419491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!