Linux之Ansible

摘要:
db'-mping192.168.175.135|SUCCESS=˃{"changed":false,"ping":"pong"}ping多个分组的服务器[root@localhostansible]#ansible'web:db'-mping192.168.175.135|SUCCESS=˃{"changed":false,"ping":"pong"}192.168.175.136|SUCCESS=˃{"changed":false,"ping":"pong"}192.168.175.137|SUCCESS=˃{"changed":false,"ping":"pong"}三、初使用首先在135服务器家目录创建一个a.sh文件#!/bin/bash#这里是指定解释器mkdir/alexchmod+xa.sh#加权限下面主控机134使用shell模块执行135服务器上的a.sh文件ansible192.168.175.135-mshell-a"basha.sh"#或者ansible192.168.175.135-mshell-a"/root/a.sh"#结果[root@localhost/]#ansible192.168.175.135-mshell-a"basha.sh"192.168.175.135|SUCCESS|rc=0˃˃也可以在135主机创建a.py,内容如下[root@localhost~]#cata.py#!
一、安装ansible

环境是centos7.0

主管服务器ip:192.168.175.134,只需安装ansible在本机即可,其余服务器无需安装,ansible通讯是用ssh

首先更换yum源

cd /etc/yum.repos.d/

cp CentOS-Base.repo CentOS-Base.repo.bak

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#  没有wget就执行yum install -y wget

yummakecache

yum install -y ansible  #  开始安装ansible

查看文档

ansible -h

更改主机列表文件

被管测试服务器ip:192.168.175.131,添加到本机ansible的hosts文件中

cd /etc/ansible/

ls#  ansible.cfg  hosts  roles

Linux之Ansible第1张

添加服务器域名或者ip,此处为本地虚拟机ip。

二、测试

尝试用ansible去ping一下被管理的虚拟机,报错

Linux之Ansible第2张

解决方案,首先用ssh连接一下被管服务器

ssh root@192.168.175.131
# 然后根据提示输入yes,再输入密码

再次用ansible去ping一下被管理的虚拟机

[root@localhost ansible]# ansible 192.168.175.131 -m ping -k
SSH password: 
192.168.175.131 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}

成功

免密登陆

ssh-keygen# 然后一路回车即可

ssh-copy-id root@192.168.175.131# 按照提示输入远程密码

ssh root@192.168.175.131# 此时已经无需密码,直接登入

二、被管机组管理

被管测试服务器:135,136,137

首先去配置各个服务器的免密登录

被管机列表文件,将其分为三组

 21[web]
 20 192.168.175.[135:136]
 21[db]                                                                                                                                          
 22 192.168.175.136
 23 192.168.175.137
 24[cache]
 25 192.168.175.137

 26## [webservers]
 27## alpha.example.org
 28## beta.example.org
 29 ## 192.168.1.100
 30 ## 192.168.1.110

查看分组服务器列表命令

[root@localhost ansible]# ansible web --list-hosts
  hosts (2):
    192.168.175.135
    192.168.175.136

ping所有主机

[root@localhost ansible]# ansible all -m ping
192.168.175.137 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.135 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.136 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}

ping多个分组

[root@localhost ansible]# ansible web,db -m ping
192.168.175.135 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.136 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.137 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}

ping多分组共有的服务器(注意单引号不要敲成双引号)

[root@localhost ansible]# ansible 'web:&db' -m ping
192.168.175.136 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}

ping一个分组中而不在另一个分组的服务器

[root@localhost ansible]# ansible 'web:!db' -m ping
192.168.175.135 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}

ping多个分组的服务器

[root@localhost ansible]# ansible 'web:db' -m ping
192.168.175.135 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.136 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
192.168.175.137 | SUCCESS =>{
    "changed": false, 
    "ping": "pong"}
三、初使用

首先在135服务器家目录创建一个a.sh文件

#!/bin/bash      #这里是指定解释器
mkdir /alex
chmod +x a.sh # 加权限

下面主控机134使用shell模块执行135服务器上的a.sh文件

ansible 192.168.175.135 -m shell -a "bash a.sh"
# 或者
ansible 192.168.175.135 -m shell -a "/root/a.sh"
# 结果
[root@localhost /]# ansible 192.168.175.135 -m shell -a "bash a.sh"
192.168.175.135 | SUCCESS | rc=0 >>

也可以在135主机创建a.py,内容如下

[root@localhost ~]# cata.py
#!/bin/envpython
#coding:utf-8print("helloworld")

主控机执行,结果

[root@localhost /]# ansible 192.168.175.135 -m shell -a "/root/a.py"
192.168.175.135 | SUCCESS | rc=0 >>helloworld

那么问题来了,我想一次性让一个组的服务器都执行,那岂不是每台服务器都要有这个sh文件

于是需要用到ansible另一个模块,script。操作db分组的主机执行主控机本机的a.sh文件,此时a.sh在主控机,不再使用被控机的文件

[root@localhost ~]# ansible db -m script -a "/root/a.sh"
192.168.175.136 | SUCCESS =>{
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.175.136 closed.
", 
    "stdout": "", 
    "stdout_lines": []
}
192.168.175.137 | SUCCESS =>{
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.175.137 closed.
", 
    "stdout": "", 
    "stdout_lines": []
}

creates选项,如果有某个文件就跳过本次执行,creates后面跟文件路径,判断被控机上是否有某个文件,此时的第二个文件路径为本地的文件,而不是被控机上的文件,这与shell和command两个模块不同,shell模块使用creates和removes判断后是执行被控机上的脚本文件

[root@localhost ~]# ansible web -m script -a "creates=/tmp /root/a.sh"
192.168.175.136 |SKIPPED
192.168.175.135 | SKIPPED

removes选项,如果没有某个文件,就跳过本次执行

[root@localhost ~]# ansible db -m script -a "removes=/root/a.sh /root/a.sh"
192.168.175.137 |SKIPPED
192.168.175.136 | SKIPPED

持续更新中。。

免责声明:文章转载自《Linux之Ansible》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SpringBoot入门 (四) 数据库访问之JdbcTemplateDebug 和 Release 编译方式的本质区别下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ping和tracert

ping命令常用于测试2台主机网络是否连通 TTL的默认值有:64(linux),128(windows),255(路由器) 此例TTL是63所以选用64来减去63等于1,这是说明经过了1个路由器,没有算上最后一个,其实总共是2个,OS是Linux。 Note:选用默认值64是因为网络中不会超过50个路由器,所以默认值的选取应该找最近的,每个默认值代表相...

Linux下的头文件搜索路径

对于以压缩包发布的软件,在它的目录下通常都有一个配置脚本configure,它的作用确定编译参数(比如头文件位置、连接库位置等),然后生成Makefile以编译程序。可以进入该软件的目录,执行"./configure --help"命令查看使用帮。 一个程序能正确编译、链接、运行需要满足3个条件:预处理时能找到头文件,连接时能找到库(静态库或动态库),运...

linux命令统计文件中某个字符串出现的次数

1、使用grep linux grep命令在我的随笔linux分类里有过简单的介绍,这里就只简单的介绍下使用grep命令统计某个文件这某个字符串出现的次数,首先介绍grep命令的几个参数,详细参数请自行找资料学习。 -a 或 --text: 不要忽略二进制的数据。 -A<显示行数> 或 --after-context=<显示行数>...

全网最详细的Git学习系列之介绍各个Git图形客户端(Windows、Linux、Mac系统皆适用ing)(图文详解)

     不多说,直接上干货! 一、TortoiseGit - The coolest Interface to Git Version Control   TortoiseGit 是 TortoiseSVN 的 Git 版本,TortoiseGit 用于迁移 TortoiseSVN 到 TortoiseGit,一直以来 Git 在 Windows 平台没...

kafka查询命令---Linux

kafka版本:kafka_2.12-2.1.1 kafka_port默认9092,zk_port默认2181 查看topicbin/kafka-topics.sh --zookeeper zk_ip:zk_port --list 查看groupbin/kafka-consumer-groups.sh --bootstrap-server kafka_ip...

Linux FTP 服务器配置简单说明

一.  FTP 说明      linux 系统下常用的FTP 是vsftp, 即Very Security File Transfer Protocol. 还有一个是proftp(Profession ftp)。 我们这里也是简单的说明下vsftp的配置。  vsftp提供3种远程的登录方式:  (1)匿名登录方式              就是不需要用...