playbook 实例

vim ~/.vimrc

autocmd FileType yaml setlocal sw=2 ts=2 et ai

变量使用及错误处理

---
- 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

 

转载于:https://www.cnblogs.com/ray-mmss/p/10419491.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值