Python Linux系统管理与自动化运维之深入浅出Ansible(三)

利用Playbook的使用并利用playbook实现Nginx与MongoDB的部署。

playbook1.yml

---
- import_playbook: playbook.yml
- hosts: db_server
  tasks:
  - name: install mongodb
    yum: name=mongodb-server state=present

- hosts: webserver
  tasks:
    - name: copy file
      copy: src=/tmp/data.txt dest=/tmp/data.txt

    - name: change mode
      file: dest=/tmp/data.txt mode=655 owner=root group=root

    - name: install file
      yum: name=httpd state=latest

ansible-playbook 命令介绍:

--list-tasks: 列出任务列表
--step: 每执行一个任务都等待用户确认
--syntax-check: 检查playbook的语法
--C --check: 检查当前这个playbook是否会修改远程服务器。

Playbook详细语法介绍:

在定义play时,只有hosts和tasks是必选选项,其他都是根据需要添加的,这里我们根据play提供的不同功能以playbook为线索介绍play与tasks中可以使用的选项

1.权限

 ---
 #针对单个任务执行管理员权限
 - hosts: webserver
   romote_user: root
   tasks:
    - name: test connection
      become: yes
      become_method: sudo
--- 
- hosts: webserver
  remote_user: root
  tasks:
    - name: test connection
    ping:
    remote_user: anan

用来检测yaml格式是否正确

#!/usr/bin/python2.7 
# -*- coding: utf-8 -*-
# @Date    : 2018-07-21 23:23:56
# @Author  : Zhou Jiaan
# @Version : V1.0
import os
import yaml
import sys

def playbookmonitor(playbook):
  with open(playbook) as f:
    print(yaml.load(f))

def main():
    try:
        test_playbook = sys.argv[1]
        print(test_playbook)
    except Exception as e:
        print(e)
        test_playbook = input("请输入需要检测的playbook:")

    else:
        print("输出test_playbook:{}".format(test_playbook))
    finally:
        playbook = test_playbook
    #    print(playbook)
        playbookmonitor(playbook)

if __name__ == '__main__':
    main()

2.通知

![](https://raw.githubusercontent.com/ananzhoujiaan/img/master/ansible_handle1.png
)

猜你喜欢

转载自blog.csdn.net/sinat_34789167/article/details/81150962