ansible 是通过python 语言开发的自动化运维工具,可以实现批量系统设置、批量程序部署、批量执行命令等功能

下面是基于docker使用ansible测试示例,可以让新手快速上手使用

一、新建4个虚拟主机

3个节点当作服务器

docker run -d --name node2 -p 2223:22 chenqionghe/ubuntu
docker run -d --name node3 -p 2224:22 chenqionghe/ubuntu
docker run -d --name node4 -p 2225:22 chenqionghe/ubuntu

一个节点安装ansible

docker run -d --name node1 -p 2222:22 \
--link node2:node2 \
--link node3:node3 \
--link node4:node4 \
chenqionghe/ubuntu

二、ssh连接node1进行准备操作

密码88888888

ssh root@127.0.0.1 -p 2222

安装ssh-copy-id

curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh
chmod 755 /usr/bin/ssh-copy-id

生成ssh公钥

ssh-keygen

设置3个节点免密码登录

ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node3
ssh-copy-id -i ~/.ssh/id_rsa.pub root@node4

注意:如果ssh-copy-id连接的主机端口不是22,可以用下面的方式连接

ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 端口号 user@host"

安装ansible

apt-get install -y ansible

配置ansible的hosts,编辑/etc/ansible/hosts,加入

[web]
node2
node3
node4

三、使用ansible

列出所有命令

ansible-doc -l

列出指定命令使用方法,如ansible-doc -s command

ansible-doc -s 命令名

使用command模块

[root@6fe45a21f868 tmp]# ansible web -m command -a 'date'
node3 | SUCCESS | rc=0 >>
Mon Jan 14 08:38:57 UTC 2019 node2 | SUCCESS | rc=0 >>
Mon Jan 14 08:38:57 UTC 2019 node4 | SUCCESS | rc=0 >>
Mon Jan 14 08:38:57 UTC 2019

使用ping模块

[root@6fe45a21f868 tmp]# ansible all -m ping

node2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
node3 | SUCCESS => {
"changed": false,
"ping": "pong"
}

使用user模块,添加用户cqh

ansible web -m user -a "name=cqh"

使用shell模块,给cqh设定密码

ansible web -m shell -a 'echo superman|passwd --stdin cqh'

使用script模块(相对路径)

cd /tmp
echo "echo \"hello cqh\" > /tmp/test.txt" > test.sh
ansible web -m script -a "test.sh"
#查看是否生效
ansible web -a "cat /tmp/test.txt"

  

关于ansible的众多模块及Playbooks的使用,小伙伴们自行脑补吧

官方文档:https://docs.ansible.com/ansible/latest/index.html

中文权威指南:http://www.ansible.com.cn/

Ansible快速上手的更多相关文章

  1. Ansible 快速上手(转)

    add by zhj: 执行Ansible(发音时,重音在最前面)命令有两种方式,一种是ad-hoc形式,另一种是playbooks,对于软件开发者来说,一般使用ad-hoc就足够了.playbook ...

  2. Ansible 快速上手

    Ansible优点: 充分利用现有设施.使用 Ansible 无需安装服务端和客户端,只要 SSH 即可.这意味着,任何一台装有 Ansible 的机器都可以成为强大的管理端.我觉得,这种去中心化的思 ...

  3. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

  4. 快速上手Unity原生Json库

    现在新版的Unity(印象中是从5.3开始)已经提供了原生的Json库,以前一直使用LitJson,研究了一下Unity用的JsonUtility工具类的使用,发现使用还挺方便的,所以打算把项目中的J ...

  5. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手

    原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...

  6. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  7. 快速上手seajs——简单易用Seajs

    快速上手seajs——简单易用Seajs   原文  http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...

  8. Git版本控制Windows版快速上手

    说到版本控制,之前用过VSS,SVN,Git接触不久,感觉用着还行.写篇博文给大家分享一下使用Git的小经验,让大家对Git快速上手. 说白了Git就是一个控制版本的工具,其实没想象中的那么复杂,咱在 ...

  9. Objective-C快速上手

    最近在开发iOS程序,这篇博文的内容是刚学习Objective-C时做的笔记,力图达到用最短的时间了解OC并使用OC.Objective-C是OS X 和 iOS平台上面的主要编程语言,它是C语言的超 ...

随机推荐

  1. maven build时报错Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

    [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ ...

  2. Bootstarp 使用布局

    实现 Bootstrap 基本布局 看到了一篇 20 分钟打造 Bootstrap 站点的文章,内容有点老,重新使用 Bootstrap3 实现一下,将涉及的内容也尽可能详细说明. 1. 创建基本的页 ...

  3. 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)

    上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...

  4. PageHelper分页插件的使用

    大家好!今天写ssm项目实现分页的时候用到pageHelper分页插件,在使用过程中出现了一些错误,因此写篇随笔记录下整个过程 1.背景:在项目的开发的过程中,为了实现所有的功能. 2.目标:实现分页 ...

  5. django 标签的使用

    首先重建一个common的app 然后创建__init__使common成为一个包   注意templatetags 名字使固定的 并在下面创建一个名字为fitter的过滤器 注册过滤器app htm ...

  6. 性能测试学习 第七课 --loadrunner中JavaVuser脚本的编写

    1.环境准备:      LoadRunner11----->对应JDK1.6版本(32位) LoadRunner12----->对应JDK1.7版本(32位) (一).JDK下载安装完成 ...

  7. python Ajax

    Ajax一.准备知识JSON1.什么是json JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSO ...

  8. [Swift]LeetCode657. 机器人能否返回原点 | Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  9. [Swift]LeetCode882. 细分图中的可到达结点 | Reachable Nodes In Subdivided Graph

    Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...

  10. [Swift]LeetCode989. 数组形式的整数加法 | Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...