Linux自动运维工具Ansible的使用
Linux自动运维工具Ansible的使用
我们熟悉这个工具后, 可以很轻松的安装k8s.
一.介绍
ansible - run a task on a target host(s)
Ansible是一个用Python开发的运维工具, 可以在本地让远程主机执行命令, 项目地址: Github源码, 中文文档
二.安装
简单上阵, 我们的主机都是ubuntu, 请使用root用户:
sudo su
apt install ansible
ansible --version
配置文件:
ls /etc/ansible
ansible.cfg hosts
三.使用
请建一台虚拟机, IP: 192.168.119.3, 虚拟机安装请查看VMware Workstation虚拟机安装
首先生成ssh密钥:
ssh-keygen -t rsa -P ""
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
之后scp公钥到远程主机, 使得主机间能够互访: scp ~/.ssh/authorized_keys root@192.168.119.3:/root/.ssh.
编辑/etc/ansible/hosts:
[jj]
192.168.119.3
开始测试:
root@xxxxxx:/etc/ansible# ansible jj -m ping
172.16.13.127 | SUCCESS => {
"changed": false,
"ping": "pong"
}
如果失败, 是因为远程主机没有安装python2:
ansible all -m raw -a "wget http://mirrors.163.com/.help/sources.list.trusty && mv -f sources.list.trusty /etc/apt/sources.list"
ansible all -m raw -a "apt update && apt install -y python2.7 python-simplejson"
接着同步时间之后, 我们再来进行进一步使用:
ansible all -a 'apt install ntpdate'
ansible all -a 'ntpdate time.windows.com'
配置文件和秘钥可额外指定:
ansible autopush -i myansible.ini --private-key m.pem -m ping
四.深入使用
ansible基本有两种: -m后接模块名, -a后接命令参数.
ansible jj -m ping
ansible all -a 'curl www.baidu.com'
如果获取模块列表:ansible-doc -l, 获取指定模块的使用帮助:ansible-doc -s ping
4.1.模块:command
一般命令操作:
ansible all -m command -a 'curl www.baidu.com'
等同于:
ansible all -a 'curl www.baidu.com'
command不支持管道, 请使用shell模块.
4.2.模块:shell
ansible all -m shell -a 'ls / | cat'
脚本传入:
ansible all -m shell -a "$1"
支持管道!
4.3.模块:copy
从本地复制文件到远程
(1) 复制文件
-a "src= dest= "
(2) 给定内容生成文件
-a "content= dest= "
从本地移动文件在远程:
ansible all -m copy -a 'src=/a.txt dest=/a/a.txt mode=600'
4.4.模块:file
file模块:设置文件的属性. 此处指远程机器的文件.
(1) 创建目录:
-a "path= state=directory"
ansible all -m file -a 'path=/test/a state=directory'
(2) 创建链接文件:
-a "path= src= state=link"
ansible all -m file -a 'src=/test/a path=/test/b state=link'
(3) 删除文件:
-a "path= state=absent“
ansible all -m file -a 'path=/test/b state=absent'
4.5.模块:fetch
从远程拿文件:
ansible all -m fetch -a 'src=/test/a.txt dest=/test'
ansible all -m fetch -a "src=$1 dest=/home/chenjh/log/$2"
不能从远程拿目录!
4.6.模块:cron
定制远程定时服务:
-a "minute=
hour=
day=
month=
weekday=
job=
name=
user=
state={present|absent}
absent是删除!
参考: 这篇文章
Linux自动运维工具Ansible的使用的更多相关文章
- Linux轻量级自动运维工具-Ansible浅析【转】
转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...
- Linux轻量级自动运维工具-Ansible浅析 转
转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...
- linux自动化运维工具Ansible saltstack Puppet、Chef、Fabric之间的对比
发现分布式是一个发展的趋势,无论是大型网站的负载均衡架构还是大数据框架部署,以及云存储计算系统搭建都离不开多台服务器的连续部署和环境搭建. 当我们的基础架构是分散式或者基于云的,并且我们经常需要处理在 ...
- 18.自动运维工具ansible
1 Ansible 介绍和架构 1.1 Ansible介绍 ansible 的名称来自科幻小说<安德的游戏>中跨越时空的即时通信工具,使用它可以在相距数光年的 距离,远程实时控制前线的舰队 ...
- 企业级LINUX自动化运维工具Ansible实战课程下载
什么是Ansible? Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量 ...
- 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操
前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...
- 自动化运维工具-Ansible之7-roles
自动化运维工具-Ansible之7-roles 目录 自动化运维工具-Ansible之7-roles Ansible Roles基本概述 Ansible Roles目录结构 Ansible Roles ...
- 自动化运维工具-Ansible之2-ad-hoc
自动化运维工具-Ansible之2-ad-hoc 目录 自动化运维工具-Ansible之2-ad-hoc Ansible ad-hoc Ansible命令模块 Ansible软件管理模块 Ansibl ...
- 自动化运维工具-Ansible之1-基础
自动化运维工具-Ansible之1-基础 目录 自动化运维工具-Ansible之1-基础 Ansible 基本概述 定义 特点 架构 工作原理 任务执行模式 命令执行过程 Ansible 安装 Ans ...
随机推荐
- Mac地址转换成long长整型 2
数据之间的转换可以使用 System.Convert Mac地址转换成long长整型 /// <summary> /// 解析长整形的数据使其转换为macID /// </sum ...
- python 使微信自动回复
https://zhuanlan.zhihu.com/p/308999073 今天是鄙人的生日,欢luo过后想写点关于itchat的文章~ (不小心暴露年龄了,是的,我已经16岁了~~) 言归正传,这 ...
- centos上安装Python并修复yum
date: 2019-07-01 18:09:53 author: headsen chen notice: 个人原创 1,安装python3.7: yum install zlib-devel b ...
- Ionic 的安装运行
1.学习前准备工作 1.必须得安装 nodejs (建议安装最新的稳定版本) 2.必须有 Angular 基础:https://www.loaderman.com/goods-1047.html 2. ...
- UI——DOM
原文链接:Introduction to the DOM Introduction The Document Object Model, usually referred to as the DOM, ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_15-细粒度授权-我的课程细粒度授权-实现
先定义接口 实现接口 service 需要通过conpanyId去查询课程的列表 定义dao 要查课程的图片 名称 等相关信息.所以使用Mybatis来实现 定义Mapper 看这个dao里面方法在哪 ...
- python判断命令执行成功
if os.system('lss') !=0: print 'Without the command'
- react 问题记录三
7.三元表达式 是否显示提交按钮(值运算用一对大括号包起来): {this.state.isTrue ? <FormItem style={{textAlign: 'right',marginT ...
- Python之Numpy库常用函数大全(含注释)(转)
为收藏学习,特转载:https://blog.csdn.net/u011995719/article/details/71080987 前言:最近学习Python,才发现原来python里的各种库才是 ...
- AutoMapper扩展帮助类
/// <summary> /// AutoMapper扩展帮助类 /// </summary> public static class AutoMapperExtension ...