Ansible 运维自动化 ( 配置管理工具 )

一、关于Ansible
Ansible是一个自动化部署工具;Ansible通过SSH协议实现远程节点和管理节点之间的通信。理论上说,只要管理员通过ssh登录到一台远程主机上能做的操作,Ansible都可以做到。Ansible是python开发的,故依赖一些python库和组件,如:paramiko,PyYaml和jinja三个关键组件。

1、安装epel源

RHEL/CentOS 6:# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpmRHEL/CentOS 7:# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2、安装Ansible

[root@n1 ~]# yum install ansible -y

3、目录结构

[root@n1 ~]# tree /etc/ansible/
/etc/ansible/
|-- ansible.cfg
|-- hosts
`-- roles1 directory, 2 files

#Ansible 定义主机、组规则的配置文件

 vim /etc/ansible/hostswww.abc.com     # 定义域名192.168.1.100   # 定义 IP192.168.1.150:37268   # 指定端口号[WebServer]           # 定义分组192.168.1.10
192.168.1.20
192.168.1.30[DBServer]            # 定义多个分组192.168.1.50
192.168.1.60Monitor ansible_ssh_port=12378 ansible_ssh_host=192.168.1.200   # 定义别名# ansible_ssh_host 连接目标主机的地址# ansible_ssh_port 连接目标主机的端口,默认 22 时无需指定# ansible_ssh_user 连接目标主机默认用户# ansible_ssh_pass 连接目标主机默认用户密码# ansible_ssh_connection 目标主机连接类型,可以是 local 、ssh 或 paramiko# ansible_ssh_private_key_file 连接目标主机的 ssh 私钥# ansible_*_interpreter 指定采用非 Python 的其他脚本语言,如 Ruby 、Perl 或其他类似 ansible_python_interpreter 解释器[webservers]         # 主机名支持正则描述www[01:50].example.com[dbservers]db-[a:f].example.com

#ansible-doc 获取帮助信息

 

ansible模块比较多,可以通过ansible-doc --help 显示帮助信息[root@n1 ~]# ansible-doc --help
Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]plugin documentation toolOptions:
-a, --all **For internal testing only** Show documentation for
all plugins.
-h, --help show this help message and exit
-j, --json **For internal testing only** Dump json metadata for
all plugins.
-l, --list List available plugins
-F, --list_files Show plugin names and their source files without
summaries (implies --list)
-M MODULE_PATH, --module-path=MODULE_PATH
prepend colon-separated path(s) to module library
(default=[u'/root/.ansible/plugins/modules',
u'/usr/share/ansible/plugins/modules'])
-s, --snippet Show playbook snippet for specified plugin(s)
-t TYPE, --type=TYPE Choose which plugin type (defaults to "module")
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exitSee man pages for Ansible CLI options or website for tutorials
https://docs.ansible.com

4、查看ansible版本

[root@n1 ~]# ansible --version
ansible 2.6.4config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.6/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

5、hosts 文件添加被管理机

[root@n1 ~]# vi /etc/ansible/hosts# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110[webservers]
m1.gree.com
m2.gree.com

6、配置/etc/hosts

192.168.1.8 n1.gree.com
192.168.1.4 m1.gree.com
192.168.1.6 m2.gree.com

7、ssh-keygen认证

[root@n1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f4:53:3b:17:15:4b:30:39:c5:1d:7e:bf:75:d7:cb:5d root@n1.gree.com
The key's randomart image is:
+--[ RSA 2048]----+
| o===|
| o+oo|
| . . oo.|
| . . . . .+|
| S o o . E|
| . o. O|
| +.|
| |
| |
+-----------------+[root@n1 ~]# ssh-copy-id -i ~/.ssh/id_rsa 192.168.1.4
The authenticity of host '192.168.1.4 (192.168.1.4)' can't be established.
RSA key fingerprint is ac:e2:3b:c8:eb:4c:af:a2:83:ac:7c:51:13:22:95:5d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.4' (RSA) to the list of known hosts.
root@192.168.1.4's password: 
Now try logging into the machine, with "ssh '192.168.1.4'", and check in:.ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.#测试登录[root@n1 ~]# ssh m1.gree.com
The authenticity of host 'm1.gree.com (192.168.1.4)' can't be established.
RSA key fingerprint is ac:e2:3b:c8:eb:4c:af:a2:83:ac:7c:51:13:22:95:5d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'm1.gree.com' (RSA) to the list of known hosts.
Last login: Fri Nov 9 14:52:03 2018 from 120.236.245.14
[root@m1 ~]# exit

二、ansible常用模块

2.1、ansible 使用格式

HOST-PATTERN        #匹配主机模式,如all表示所有主机
-m MOD_NAME         #模块名   如:ping
-a MOD_ARGS         #模块执行的参数
-f FORKS            #生成几个子进行程执行 -C #(不执行,模拟跑) -u Username #某主机的用户名 -c CONNection #连接方式(default smart) 

示例:

#查看IP地址

[root@n1 ~]# ansible all -m shell -a "ifconfig"
m2.gree.com | SUCCESS | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 52:54:00:2F:CA:4A  inet addr:192.168.1.6  Bcast:192.168.1.255  Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1RX packets:60690 errors:0 dropped:0 overruns:0 frame:0TX packets:53579 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:15046135 (14.3 MiB)  TX bytes:6350897 (6.0 MiB)lo        Link encap:Local Loopback  inet addr:127.0.0.1  Mask:255.0.0.0UP LOOPBACK RUNNING  MTU:65536  Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)m1.gree.com | SUCCESS | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 52:54:00:98:4B:1D  inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1RX packets:42579 errors:0 dropped:0 overruns:0 frame:0TX packets:32927 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:27319373 (26.0 MiB)  TX bytes:5283478 (5.0 MiB)lo        Link encap:Local Loopback  inet addr:127.0.0.1  Mask:255.0.0.0UP LOOPBACK RUNNING  MTU:65536  Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

2.2、copy模块
从本地copy文件分发到目录主机路径 
参数说明:
src= 源文件路径
dest= 目标路径 
注意src= 路径后面带/ 表示带里面的所有内容复制到目标目录下,不带/是目录递归复制过去
content= 自行填充的文件内容
owner 属主
group 属组
mode权限

[root@n1 tmp]# ansible all -m copy -a "src=/tmp/1.txt dest=/tmp/1.txt mode=644"
m2.gree.com | SUCCESS => {"changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/1.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1541754612.03-46738208597425/source", "state": "file", "uid": 0
}
m1.gree.com | SUCCESS => {"changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/1.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1541754612.03-181393664674709/source", "state": "file", "uid": 0
}

2.3 fetch模块
从远程主机拉取文件到本地
示例

[root@n1 tmp]# ansible all -m fetch -a "src=/tmp/2.txt dest=/tmp"
m1.gree.com | SUCCESS => {"changed": false, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/m1.gree.com/tmp/2.txt", "file": "/tmp/2.txt", "md5sum": "d41d8cd98f00b204e9800998ecf8427e"
}
m2.gree.com | SUCCESS => {"changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/tmp/m2.gree.com/tmp/2.txt", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "remote_md5sum": null
}
[root@n1 tmp]# ll
total 24
-rw-r--r-- 1 root root    0 Nov  9 17:09 1.txt
-rw-r--r-- 1 root root 3018 Nov  9 14:48 cvm_init.log
drwxr-xr-x 3 root root 4096 Nov  9 17:19 m1.gree.com
drwxr-xr-x 3 root root 4096 Nov  9 17:20 m2.gree.com
-rw-r--r-- 1 root root  797 Nov  9 14:48 net_affinity.log
-rw-r--r-- 1 root root   26 Nov  9 14:48 nv_gpu_conf.log
-rw-r--r-- 1 root root  192 Nov  9 14:48 setRps.log
[root@n1 tmp]# cd m2.gree.com/
[root@n1 m2.gree.com]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov  9 17:20 tmp
[root@n1 m2.gree.com]# cd tmp/
[root@n1 tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Nov  9 17:20 2.txt

2.3、command模块
在远程主机上执行命令,属于裸执行,非键值对显示;不进行shell解析;

[root@n1 tmp]# ansible all -m command -a "ifconfig"
m2.gree.com | SUCCESS | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 52:54:00:2F:CA:4A  inet addr:192.168.1.6  Bcast:192.168.1.255  Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1RX packets:85804 errors:0 dropped:0 overruns:0 frame:0TX packets:78728 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:17782454 (16.9 MiB)  TX bytes:9652720 (9.2 MiB)lo        Link encap:Local Loopback  inet addr:127.0.0.1  Mask:255.0.0.0UP LOOPBACK RUNNING  MTU:65536  Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

2.4、shell模块
由于commnad只能执行裸命令(即系统环境中有支持的命令),至于管道之类的功能不支持,
shell模块可以做到

[root@n1 tmp]# ansible all -m shell -a "ifconfig|grep lo"
m1.gree.com | SUCCESS | rc=0 >>
lo        Link encap:Local Loopback  m2.gree.com | SUCCESS | rc=0 >>
lo        Link encap:Local Loopback  

2.5、file模块
设置文件属性(创建文件)
常用参数:
path目标路径
state directory为目录,link为软件链接
group 目录属组
owner 属主
等,其他参数通过ansible-doc -s file 获取
示例1:创建目录

[root@n1 tmp]# ansible all -m file -a "path=/tmp/hello state=directory"
m1.gree.com | SUCCESS => {"changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/hello", "size": 4096, "state": "directory", "uid": 0
}

2.6、

 

参考:

http://blog.51cto.com/dyc2005/2070729

 https://www.cnblogs.com/wangxiaoqiangs/p/5685239.html

转载于:https://www.cnblogs.com/nulige/p/9936091.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/364978.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分解 python_面试官:如何用Python实现将一个整数分解成质因数?

概述今天主要分享一个关于分解质因数的实例&#xff0c;判断的逻辑稍微多了点&#xff0c;一起来看看吧~需求将一个整数分解质因数。例如&#xff1a;输入90,打印出90233*5思路其实根本不需要判断是否是质数&#xff0c;从2开始向数本身遍历&#xff0c;能整除的肯定是最小的质…

题解 P5259【欧稳欧再次学车】

实际上没什么可说的&#xff0c;暴力大模拟就好。 一定要开long long&#xff01; 一定要开long long&#xff01; 一定要开long long&#xff01; &#xff08;不然会炸数据的&#xff01;&#xff01;&#xff01;&#xff09; //Stand up for the faith! #include<bits/s…

如何:在Maven项目(JUnit,Mockito,Hamcrest,AssertJ)中测试依赖项

对于当今的大多数Java项目&#xff0c;JUnit本身还远远不够。 您还需要一个模拟库&#xff0c;也许还有其他东西。 在此迷你操作指南中&#xff0c;我介绍了可以在新的Java项目中开始的测试依赖项。 一切都始于JUnit Maven存储库中的junit组中有两个工件&#xff1a; junit和j…

elementUI vue 编辑中的input的验证残留清除

当使用编辑的时候, 假如上次的验证没通过, 报红了, 下次再点击编辑的时候还会报红,因此要清除验证残留, 方式有两种: this.$refs["from"].resetFields(); //移除校验结果并重置字段值this.$refs["from"].clearValidate(); //移除校验结果 更多专业前端知识…

Kali学习笔记25:Arachni使用(实现分布式扫描)

文章的格式也许不是很好看&#xff0c;也没有什么合理的顺序 完全是想到什么写一些什么&#xff0c;但各个方面都涵盖到了 能耐下心看的朋友欢迎一起学习&#xff0c;大牛和杠精们请绕道 Arachni不同于上次介绍的nikto和skipfish 是一个Web界面的一个Web扫描器 Arachni的强大不…

红帽正式宣布发布JBoss BPM Suite 6和JBoss BRMS 6

红帽公司刚刚宣布了这些期待已久的产品的全面上市 &#xff01; 要花费大量精力将社区代码转变为企业质量的软件&#xff0c;客户和最终用户可以在Red Hat支持的生产环境中使用这些代码。 现在是现在和潜在客户学习该产品&#xff0c;让合作伙伴开始使用它并学习基本知识&…

三星sd卡无法删除数据_单反相机里的SD卡在电脑中突然无法识别提示要格式化怎么办?...

大家在这个国庆佳节出去游玩都拍了不少精彩照片吧&#xff0c;昨天就有微信好友求助&#xff0c;说刚刚从外面游玩回到家里&#xff0c;准备把单反相机里64G存储SD卡中的照片用读卡器复制到电脑里&#xff0c;插入电脑后就提示如下图&#xff1a;在电脑中只显示“可移动磁盘”&…

CF1080F Katya and Segments Sets

题目链接&#xff1a;洛谷 题目描述&#xff1a;【看翻译】 这种强制在线的方法可真是奇妙。 主席树可真是奇妙。 我们用主席树的版本维护$x\leq l$的限制&#xff0c;用线段树维护$[a,b]$的限制&#xff0c;用节点的值来维护$r\leq y$的限制。 详细地说&#xff0c;就是先将线…

在微信小程序上,帮助中心界面实现类似手风琴案例

小程序wxml代码如下: <block wx:for"{{arrdata}}" wx:key""><view class"centent_title" tap"open_that" data-index"{{index}}"><view class"title" >{{item.name}}?</view><image…

python 等待其他线程执行完_面试官:如何让线程顺序执行,join,还有其他办法吗?...

面试官&#xff1a;如让线程顺序执行&#xff1f;我&#xff1a;使用Thread的join方法。面试官&#xff1a;除了join还有别的办法吗&#xff1f;我&#xff1a;目前只用过join。面试官&#xff1a;哦&#xff0c;那你了解CountDownLatch吗&#xff1f;我&#xff1a;不了解&…

计算斐波那契数列

O(n)复杂度方法O(logn)复杂度方法计算矩阵的n次方&#xff0c;可以先求矩阵的n/2次方&#xff0c;然后再平方即可。 1 #include <cassert>2 3 struct Matrix2By24 {5 Matrix2By26 (7 long long m00 0,8 long long m01 0,9 long long m…

使用ANTLR4,用于代码镜像和基于Web的DSL的Primefaces扩展

DSL是很酷的东西&#xff0c;但是我不清楚它们有什么用。 然后我意识到它们对以下方面有好处&#xff1a; 摆脱复杂的UI 意思是 更快的做事方式 而已。 当我阅读此博客时&#xff0c;我得出了这个结论。 如果您的用户是技术人员&#xff0c;并且不惧怕类似SQL的语法处理方…

js 动态绑定事件 on click 完美解决绑定不成功

动态绑定坑了多少人..... //绑定 $("ol").on("click","li a",function(){ ... }) //解绑 $("ol").off("click","li a"); 完美解决动态绑定: ol 为动态生成html的父类. li a 为其动态生成的html, 绑定使用on…

连接mysql超时时间设置多少_怎么设置数据库的连接数和连接超时时间

如何设置数据库的连接数和连接超时时间连接数的话可以修改spfile文件来约束查看当前的连接数&#xff1a;select count(*) from v$process;–数据库允许的最大连接数&#xff1a;select value from v$parameter where name ‘processes’&#xff1b;–修改最大连接数&#xf…

【学习笔记】慕课网—Java设计模式精讲 第3章 软件设计七大原则-3-4 单一职责原则...

/** * 软件设计七大原则-单一职责原则 学习笔记 * author cnRicky * date 2018.11.10 */单一职责原则 定义&#xff1a;不要存在多于一个导致类变更的原因一个类只负责一个职责&#xff0c;如果分别有两个职责&#xff0c;那就建立两个类分别负责职责1和职责2一个类/接口/方法只…

移动架构-数据库分库和全版本升级

在项目中&#xff0c;往往涉及到数据库的版本升级&#xff0c;通常会有两种升级方式&#xff0c;一种是纯代码实现&#xff0c;一种是脚本实现&#xff0c;这里使用脚本升级&#xff0c;这样的升级方式更便于维护 思路 全版本升级&#xff0c;重点在于数据的迁移&#xff0c;这…

Java中的硬件事务性内存,或者为什么同步将再次变得很棒

总览 硬件事务内存有潜力允许多个线程同时以推测方式访问相同的数据结构&#xff0c;并使缓存一致性协议确定是否发生冲突。 HTM旨在为您提供细粒度锁定的可伸缩性&#xff0c;粗粒度锁定的简单性以及几乎没有锁定的性能。 如果JVM支持&#xff0c;则您的程序或库是使用过程粒度…

mysql5.7乱码_mysql5.7中解决中文乱码的问题

在使用mysql5.7时&#xff0c;会发现通过web端向数据库中写入中文后会出现乱码&#xff0c;但是在数据库中直接操作SQL语句插入数据后中文就显示正常&#xff0c;这个问题怎么解决呢&#xff1f;此处不对mysql的数据编码过程和原理进行讲解&#xff0c;如果有兴趣的同学可以自己…

a 链接点击下载

1. 将链接设置为.zip 结尾2.在a元素中添加download 属性,&#xff08;目前只有chrome、firefox和opera支持&#xff09; function download(src) { var $a $("<a></a>").attr("href", src).attr("download", "img.png");…

Codeforces Global Round 2 D. Frets On Fire (动态开点线段树,沙雕写法)

题目链接&#xff1a;D. Frets On Fire 思路&#xff1a;明明可以离散化二分写&#xff0c;思路硬是歪到了线段树上&#xff0c;自闭了&#xff0c;真实弟弟&#xff0c;怪不得其他人过得那么快 只和查询的区间长度有关系&#xff0c;排完序如果相邻的两个点的差值小于等于查询…