Linux自动运维工具Ansible的使用

我们熟悉这个工具后, 可以很轻松的安装k8s.

一.介绍

ansible - run a task on a target host(s)

Ansible是一个用Python开发的运维工具, 可以在本地让远程主机执行命令, 项目地址: Github源码, 中文文档

二.安装

简单上阵, 我们的主机都是ubuntu, 请使用root用户:

  1. sudo su
  2. apt install ansible
  3. ansible --version

配置文件:

  1. ls /etc/ansible
  2. ansible.cfg hosts

三.使用

请建一台虚拟机, IP: 192.168.119.3, 虚拟机安装请查看VMware Workstation虚拟机安装

首先生成ssh密钥:

  1. ssh-keygen -t rsa -P ""
  2. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

之后scp公钥到远程主机, 使得主机间能够互访: scp ~/.ssh/authorized_keys root@192.168.119.3:/root/.ssh.

编辑/etc/ansible/hosts:

  1. [jj]
  2. 192.168.119.3

开始测试:

  1. root@xxxxxx:/etc/ansible# ansible jj -m ping
  2. 172.16.13.127 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }

如果失败, 是因为远程主机没有安装python2:

  1. ansible all -m raw -a "wget http://mirrors.163.com/.help/sources.list.trusty && mv -f sources.list.trusty /etc/apt/sources.list"
  2. ansible all -m raw -a "apt update && apt install -y python2.7 python-simplejson"

接着同步时间之后, 我们再来进行进一步使用:

  1. ansible all -a 'apt install ntpdate'
  2. ansible all -a 'ntpdate time.windows.com'

配置文件和秘钥可额外指定:

  1. ansible autopush -i myansible.ini --private-key m.pem -m ping

四.深入使用

ansible基本有两种: -m后接模块名, -a后接命令参数.

  1. ansible jj -m ping
  2. ansible all -a 'curl www.baidu.com'

如果获取模块列表:ansible-doc -l, 获取指定模块的使用帮助:ansible-doc -s ping

4.1.模块:command

一般命令操作:

  1. ansible all -m command -a 'curl www.baidu.com'

等同于:

  1. ansible all -a 'curl www.baidu.com'

command不支持管道, 请使用shell模块.

4.2.模块:shell

  1. ansible all -m shell -a 'ls / | cat'

脚本传入:

  1. ansible all -m shell -a "$1"

支持管道!

4.3.模块:copy

从本地复制文件到远程

(1) 复制文件

  1. -a "src= dest= "

(2) 给定内容生成文件

  1. -a "content= dest= "

从本地移动文件在远程:

  1. ansible all -m copy -a 'src=/a.txt dest=/a/a.txt mode=600'

4.4.模块:file

file模块:设置文件的属性. 此处指远程机器的文件.

(1) 创建目录:

  1. -a "path= state=directory"
  2. ansible all -m file -a 'path=/test/a state=directory'

(2) 创建链接文件:

  1. -a "path= src= state=link"
  2. ansible all -m file -a 'src=/test/a path=/test/b state=link'

(3) 删除文件:

  1. -a "path= state=absent“
  2. ansible all -m file -a 'path=/test/b state=absent'

4.5.模块:fetch

从远程拿文件:

  1. ansible all -m fetch -a 'src=/test/a.txt dest=/test'
  1. ansible all -m fetch -a "src=$1 dest=/home/chenjh/log/$2"

不能从远程拿目录!

4.6.模块:cron

定制远程定时服务:

  1. -a "minute=
  2. hour=
  3. day=
  4. month=
  5. weekday=
  6. job=
  7. name=
  8. user=
  9. state={present|absent}

absent是删除!

参考: 这篇文章

Linux自动运维工具Ansible的使用的更多相关文章

  1. Linux轻量级自动运维工具-Ansible浅析【转】

    转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...

  2. Linux轻量级自动运维工具-Ansible浅析 转

    转自 Linux轻量级自动运维工具-Ansible浅析 - ~微风~ - 51CTO技术博客http://weiweidefeng.blog.51cto.com/1957995/1895261 Ans ...

  3. linux自动化运维工具Ansible saltstack Puppet、Chef、Fabric之间的对比

    发现分布式是一个发展的趋势,无论是大型网站的负载均衡架构还是大数据框架部署,以及云存储计算系统搭建都离不开多台服务器的连续部署和环境搭建. 当我们的基础架构是分散式或者基于云的,并且我们经常需要处理在 ...

  4. 18.自动运维工具ansible

    1 Ansible 介绍和架构 1.1 Ansible介绍 ansible 的名称来自科幻小说<安德的游戏>中跨越时空的即时通信工具,使用它可以在相距数光年的 距离,远程实时控制前线的舰队 ...

  5. 企业级LINUX自动化运维工具Ansible实战课程下载

    什么是Ansible? Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量 ...

  6. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操

    前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...

  7. 自动化运维工具-Ansible之7-roles

    自动化运维工具-Ansible之7-roles 目录 自动化运维工具-Ansible之7-roles Ansible Roles基本概述 Ansible Roles目录结构 Ansible Roles ...

  8. 自动化运维工具-Ansible之2-ad-hoc

    自动化运维工具-Ansible之2-ad-hoc 目录 自动化运维工具-Ansible之2-ad-hoc Ansible ad-hoc Ansible命令模块 Ansible软件管理模块 Ansibl ...

  9. 自动化运维工具-Ansible之1-基础

    自动化运维工具-Ansible之1-基础 目录 自动化运维工具-Ansible之1-基础 Ansible 基本概述 定义 特点 架构 工作原理 任务执行模式 命令执行过程 Ansible 安装 Ans ...

随机推荐

  1. linux的常见目录

    常见的目录和作用 目录名 目录名的作用 /bin/ 存放系统命令的目录,普通用户和root都使用,不过放在bin/命令下的单用户模式也可执行 /sbin/ 保存于系统环境相关的命令,只有root可以使 ...

  2. 阿里云服务器配置https(总结)

    阿里云服务器配置https(总结) 一.总结 一句话总结: 1.下载https证书(可以在阿里云上) 2.在服务器上面开启443端口 3.配置apache服务器,443的加ssl,让80的重定向到44 ...

  3. 咏南中间件D7客户端演示

    咏南中间件D7客户端演示 咏南中间件MORMOT(http.sys)支持D6.D7等老版本开发客户端.客户端使用TClientDataSet内存表控件,数据序列使用TynSerial类.

  4. 用html+css+js实现一个无限级树形控件

    https://blog.csdn.net/cc_fys/article/details/81284638 树形菜单示例: <!DOCTYPE html> <head> < ...

  5. Web前端笔记整理

    不使用Ajax无刷新提交: header('HTTP/1.1 204 No Content'); var a=document.createElement('img'); a.setAttribute ...

  6. SQL-W3School-函数:SQL NOW() 函数

    ylbtech-SQL-W3School-函数:SQL NOW() 函数 1.返回顶部 1. NOW() 函数 NOW 函数返回当前的日期和时间. 提示:如果您在使用 Sql Server 数据库,请 ...

  7. PorterDuffXfermode之PorterDuff.Mode.MULTIPLY

    package com.loaderman.customviewdemo.view; import android.content.Context; import android.graphics.B ...

  8. PorterDuffXfermodeMode.DST_IN

    package com.loaderman.customviewdemo.view; import android.animation.ValueAnimator; import android.co ...

  9. Linux 验证当前 Video0 不否是v4l设备 linux v4l 编程(1) Video 4 Linux 简介

    #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h ...

  10. mysqlcheck修复工具

    mysqlcheck工具可以检查.修复.优化和分析MyISAM引擎的表,实际上是集成了Mysql中的check.repair.analyze.tmpimize的功能. mysqlcheck共军存在于m ...