码农之家
这篇文章主要知识点是关于Python Linux系统管理与自动化运维,Ansible,Python Linux系统管理与自动化运维,《Python Linux系统管理与自动化运维》学习总结 python模块库ansible的用法详解 python将ansible配置转为json格式的实例方法总结 的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
Ansible维护模式通常由控制机和被管理机组成。因为Ansible的特性不像其他运维工具那样需要在被监控的目标上安装agent,所以我们的侧重点只需要放在安装了Ansible的服务器上(当然用于实验环境的话可以是一台笔记本,台式机或者仅仅只需要一台虚拟机即可)。Ansible可以通过SSH进行远程链接到对端主机上,然后执行你想要执行的playbook或者指令。所有的远程主机都可以在inventory中被定义。
ansible test -m ping #测试ansible是否正常 ansible test -a "whoami" ansible test -a "hostname"
ansible test -m copy -a "src=/tmp/data.txt dest=/tmp/data.txt" #实现将本地的/tmp/data.txt 拷贝到远程主机tmp/data.txt中
file 将权限改为700 所有者和所属组改为anan
ansible test -m file -a"dest=/tmp/data.txt mode=700 owner=anan group=anan"
ansible test -m yum -a "name=lrzsz state=present " -become #become类似于sudo 安装lrzsz
ansbile-playbook test_playbook.yml
--- - hosts: test tasks: - name: test ping ping: - name: test copy file copy: src=/tmp/data.txt dest=/tmp/data2.txt - name: test change file mode file: dest=/tmp/data2.txt mode=700 owner=anan group=anan - name: test install software yum: name="lrzsz" state=latest #absent present removed latest
ansible test --list-hosts\ ansible test -i hostsdir
远程服务器匹配规则
hosts文件如下:
[user:children] webserver db_server monitor_server docker_server master [webserver:children] http_server nginx_server [db_server:children] mysql_server redis_server [monitor_server:children] nagios_server zabbix_server [container_server:children] docker_server [master] 10.1.11.34 [http_server] 10.1.11.35 [http_server:vars] http_port=80 maxRequestsPerChild=808 [nginx_server] 10.1.11.36 [mysql_server] 10.1.11.37 [redis_server] [nagios_server] [zabbix_server] [docker_server] [test] 10.1.11.36
hosts表: create table hosts( id int not null auto_increment, host varchar(15) default null, groupname varchar(15) default null, username varchar(15) default null, port int(11) default '22', primary key(id) )engine=InnoDB auto_increment=3 default charset=utf8mb4; insert into hosts (host,groupname,username) values(('10.1.11.35','anan','anan'),('10.1.11.36','anan','anan'),('10.1.11.37','anan','anan'));
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/20 16:20 # @Author : anan # @Site : # @File : hosts.py # @Software: PyCharm from __future__ import print_function import argparse import json from collections import defaultdict from contextlib import contextmanager import pymysql def to_json(in_dict): return json.dumps(in_dict,sort_keys=True,indent=2) @contextmanager def get_conn(**kwargs): conn=pymysql.connect(**kwargs) try: yield conn finally: conn.close() def parse_args(): parser=argparse.ArgumentParser(description="Openstack Inventory Module") group=parser.add_mutually_exclusive_group(required=True) group.add_argument('--list',action='store_true',help='List active server') group.add_argument('--host',help="List details about the specific host") return parser.parse_args() def list_all_host(conn): hosts=defaultdict(list) with conn as cur: cur.execute('select * from hosts') rows=cur.fetchall() for row in rows: no,host,group,user,port=row hosts[group].append(host) return hosts def get_host_detail(conn,host): details={} with conn as cur: cur.execute("select * from hosts where host='{0}'".format(host)) rows=cur.fetchall() if rows: no,host,group,user,port=rows[0] details.update(ansible_user=user,ansible_port=port) return details def main(): parser=parse_args() with get_conn(host='10.1.11.37',user='root',passwd='passwd',db='test') as conn: if parser.list: hosts=list_all_host(conn) print(to_json(hosts)) else: details=get_host_detail(conn,parser.host) print(to_json(details)) if __name__ == '__main__': main()
yaml文件编辑:
palybook2.yml
--- #一个职工的记录 name: examaple developer job: developer skill: elite employed: True foods: - Apple - Orange - Strawberry - Mango Language: ruby: elite Python: elite dotnet: Lame
*ansible目前已经提供了超过950个模块:用户可以根据自己喜欢的编程语言编写第三方模块(学习和使用Ansible模块)*
ansible-doc file
ansible-doc -l file
以上就是本次给大家分享的全部知识点内容总结,大家还可以在下方相关文章里找到vue项目中使用md5加密以及、 详解vue axios封装请求状态、 儿童python编程入门书籍推、 等python文章进一步学习,感谢大家的阅读和支持。
展开 +
收起 -
Copyright 2018-2019 xz577.com 码农之家