ansible简单用法

ansible简介:

Ansible 是一个开源的基于 OpenSSH 的自动化配置管理工具。可以用它来配置系统、部署软件和编排更高级的 IT 任务,比如持续部署或零停机更新。
Ansible 的主要目标是简单和易用,
并且它还高度关注安全性和可靠性。基于这样的目标,
Ansible 适用于开发人员、系统管理员、发布工程师、IT 经理,以及介于两者之间的所有人。Ansible 适合管理几乎所有的环境,
从拥有少数实例的小型环境到有数千个实例的企业环境。
使用 Ansible 无须在被管理的机器上安装代理,所以不存在如何升级远程守护进程的问题,也不存在由于卸载了守护进程而无法管理系统的问题。

ansible主要功能:

自动化:减轻运维工作压力,提高工作效率  实现批量部署
ansible和远程主机有连接是因为有ssh协议    ansible要对远程主机进行免密登录
特点:
    1.无客户端模式   只是在服务端安装服务
    2. 通过ssh协议和客户端之间进行联系的
    3. 服务端分发任务之间进行联系的
ansible的优势:----》  puppet   (比较)
可以逐行的执行 shell 命令。
不需要另外的客户端工具(linux 一般会自带 ssh 工具)。
相同的配置只被执行一次(多次执行同一配置不会出问题)。

核心模块:
1.ansible core 内核
2.host inventory 主机清单
3. connection plugins ssh
4. playbook 剧本 role角色
5. core modules 核心模块
6. custom modules 自定义模块 (二开)

www.ansible.com  官网
www.ansbile.cn    中国社区的网站     ansible的文档

自动化类型:

系统自动化:pxe+ks
程序自动化:ansible   saltstack   puppet
监控自动化:zabbix  天兔(lepus)  [  lepus   部署和使用](https://blog.csdn.net/weixin_43557605/article/details/89502546)      
代码自动化:jenkins
puppet       基于ruby语言开发的    linux   unix    windows    1000台以上的主机
saltstack    基于python语言开发的  支持统一以管理   比较轻量级    1000台以上的主机
ansbile      基于python语言开发的   使用ssh协议进行管理  100台主机   200台

工作原理:

主控生成一对密钥,将公钥传递到远程主机上,当主控端想要连接远程主机时,
远程主机会随机发送一串字符串给主控端,主控端将这串字符用私钥加密
返回给远程主机,远程主机使用公钥密钥进行解密,
如果和生成的字符一致,则验证通过,可以登录

安装ansible

 虚拟机联网 :
 yum -y  install  ansible

如果找不到有的软件包,
解决办法:

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo [root@localhost ~]# https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo
[root@localhost ~]# sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# sed -i "s@http://mirror.centos.org@https://mirrors.huaweicloud.com@g" /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache  fast

出现元数据已经建立即可,再次安装ansible

生成ssh公钥认证所需的公钥和私钥文件

[root@bb ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):    #密钥对存放路径
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):        #输入私钥保护密码,直接按Enter表示无密码
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:
99:0d:e7:0a:de:ee:76:ac:5c:af:15:ab:3d:bb:b5:75 root@bb
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        . .      |
|         B       |
|      . S o .    |
|     . o .   o   |
|      . o.. o . E|
|       o..o=.. o.|
|       o=oo.=+.  |
+-----------------+

注:ssh-keygen = ssh-keygen -t t指定密钥的类型,默认就是rsa,所以不加-t rsa也行,如果你想生成dsa密钥,就需要加参数-t dsa。
将公钥发送给两个远程主机

[root@bb ~]# cd   /root/.ssh    #查看生成密钥对
[root@bb .ssh]# ls
id_rsa  id_rsa.pub
[root@bb .ssh]# ssh-copy-id root@192.168.20.132
The authenticity of host '192.168.20.132 (192.168.20.132)' can't be established.
ECDSA key fingerprint is 41:73:0e:ae:48:52:36:b8:b4:8b:aa:d0:a8:91:4b:1a.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.132's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.20.132'"
and check to make sure that only the key(s) you wanted were added.


[root@bb .ssh]# ssh-copy-id root@192.168.20.133
The authenticity of host '192.168.20.133 (192.168.20.133)' can't be established.
ECDSA key fingerprint is 7a:31:f1:a7:28:c7:7f:a6:a2:b0:f1:23:58:23:58:5a.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.133's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.20.133'"
and check to make sure that only the key(s) you wanted were added.

查看远程主机密钥存放位置

root@cc ~]# cd   /root/.ssh/
[root@cc .ssh]# ls
authorized_keys                     #密钥文件
[root@dd ~]# cd  /root/.ssh/
[root@dd .ssh]# ls
authorized_keys

验证是否可以免密登录

[root@bb .ssh]# ssh   root@192.168.20.132
Last login: Mon Mar 30 19:19:45 2020
[root@cc ~]# exit
登出
Connection to 192.168.20.132 closed.
[root@bb .ssh]# ssh   root@192.168.20.133
Last login: Mon Mar 30 19:18:47 2020
[root@dd ~]# exit
登出
Connection to 192.168.20.133 closed.

如果想要删除密钥  就是把.ssh目录删除掉

通过配置/etc/ansible/hosts这个文件来定义主机和组

[root@bb ~]# vim   /etc/ansible/hosts 
[webserver]      #清单名称
192.168.20.132    #远程主机ip
192.168.20.133
[dbserver]
192.168.20.132
[test1]
www.benet.com:222          #ssh默认通过22端口进行其管理设备     如果目标主机没有使用22端口   ,我们可以在主机名称后面使用冒号加端口号表明
[test2]
zs1.kgc.cn
zs[2:5].kgc.cn                 #[2:5]表示2~5之间的所有数字,即表示zs2.kgc.cn,zs3.kgc.cn,zs4.kgc.cn,zs5.kgc.cn         

使用模块对后端主机进行操作的命令格式

ansible    hosts(主机清单)    -m    module_name  (模块名)  -a  job(对后端主机进行什么样的操作)
ansible的执行结果
green   成功
red       失败
yellow    成功    并且对后端进行了更改
紫色          警告

列出ansible所有的模块

[root@bb ~]# ansible-doc -l      # #列出模块信息

输入q退出 如果死机了 终端关掉

查看模块的帮助信息

[root@bb ~]#  ansible-doc -s ping       # -s  加模块名
- name: Try to connect to host, verify a usable python and return `pong' on succ
  ping:
      data:                  # Data to return for the `ping' return value. If
                               this parameter is
                               set to `crash',
                               the module will
                               cause an
                               exception.
(END)

ansible的参数:
-v:输出执行过程的信息,得到执行过程中的所有信息
-i: 指定 inventory信息 默认是/etc/ansible/hosts
-f:并发线程数,默认为五个线程
-m:指定执行使用的模块
-M:指定默认存放的路径,默认为/etc/share/ansible 也可以通过ANSIBLE_LIBRARY设定默认路径
-a:指定模块参数
-u:指定远程主机以USERNAME运行命令
-l:限制运行主机
–list-hosts:列出符合条件的主机列表,不执行任何命令

模块用法

通过对定义的组进行操作
1.ping 测试主控端和远程主机是否能够连通 ssh

[root@bb ~]# ansible webserver -m ping 
192.168.20.132 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.20.133 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

all 所有主机清单中的所有主机

[root@bb ~]# ansible all -m  ping        #all    所有主机清单中的所有主机
192.168.20.132 | SUCCESS => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"
   }, 
   "changed": false, 
   "ping": "pong"
}
192.168.20.133 | SUCCESS => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"
   }, 
   "changed": false, 
   "ping": "pong"
}

也可以使用针对组中某一个或多个主机操做
``

  1. 只针对webserver组中的主机192.168.20.132进行操作。通过–limit参数限定主机的变更
     [root@bb ~]# ansible all -m  ping  --limit "192.168.20.132"
     192.168.20.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

2)只针对192.168.20.133进行操作。 通过ip限定主机的变更

       [root@bb ~]# ansible 192.168.20.133 -m  ping   
192.168.20.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  1. 只针对192.168.20.0网段的主机操作 。 通过通配符限定变更
     [root@bb ~]# ansible 192.168.20.* -m  ping   
192.168.20.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.20.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

2.command 在远程主机上执行指定命令 不能使用特殊的符号 | > >>

[root@bb ~]# ansible all -m  command  -a "ls /home"   
192.168.20.133 | CHANGED | rc=0 >>
tom
192.168.20.132 | CHANGED | rc=0 >>
tom

参数:
chdir :切换目录 (在远程主机上要提前进入的目录)
creates 当指定文件存在时 ,命令不执行 当指定文件不存在时 命令执行
removes 当指定文件存在时 ,命令执行 当指定文件不存在时 命令不执行
executeble:指明运行命令的shell程序

[root@bb ~]# ansible all -m  command  -a "chdir=/home ls  ./"
192.168.20.133 | CHANGED | rc=0 >>
tom
192.168.20.132 | CHANGED | rc=0 >>
tom
[root@bb ~]# ansible all -m  command  -a "creates=/etc/fstab ls /home" 
[root@bb ~]# ansible all -m  command  -a "creates=/etc/fstabhdfksh ls /home"
[root@bb ~]# ansible all -m  command  -a "removes=/etc/fstabhdfks3h ls /home"
[root@bb ~]# ansible all -m  command  -a "removes=/etc/fstab ls /home"

3.shell 万能模块 在远程主机上执行指定的命令

root@bb ~]# ansible  all  -m   shell  -a  "touch /home/bb"
[root@bb ~]# ansible  all  -m   shell  -a  "ls  /home |  grep  bb"
[root@bb ~]# ansible  all  -m   shell  -a  "echo '123' >  /home/bb"
[root@bb ~]# ansible  all  -m   shell  -a  "cat /home/bb"

4.user 管理或者创建远程主机上的用户
name 指定用户名 如果用户不存在 则创建该用户

[root@bb ~]# ansible all  -m user   -a "name=aa"
[root@bb ~]# ansible all  -m shell -a  "tail -1  /etc /passwd"
              password     给用户添加密码   修改密码    添加密码的时候只能识别加密后的字符
[root@bb ~]# yum  -y  install  openssl-devel
[root@bb ~]# openssl passwd   -1  123.com       #使用MD5方式进行加密
$1$Y5QObVQZ$BTHlwQCHCuLJyds6SO8PA.
[root@bb ~]# ansible all  -m user   -a 'name=aa password=$1$Y5QObVQZ$BTHlwQCHCuLJyds6SO8PA.'
注意:这个需要使用单引号   否则可能会失败

远程主机上验证

[root@cc ~]# su  tom
[tom@cc root]$ su  aa
密码:
bash-4.2$ 

参数:
uid 指定用户的uid
group 指定用户的基本组
groups 指定用户的附加组
append=yes 增加附加组 相当于把用户添加到另一个附加组中
append=no 全量添加附加组 相当于只设置一个附加组

基本组
[root@bb ~]# ansible   all  -m   user -a  "group=aa name=test"
[root@bb ~]# ansible all -m  shell -a "tail -2  /etc/passwd"

附加组
ansible  all  -m  user  -a  "groups=test name=test"
ansible all -m  shell -a "tail -2  /etc/group"

增量添加
ansible  all  -m  shell  -a  "groupadd one"
ansible  all  -m  user  -a  "groups=one   append=yes  name=test"
ansible all -m  shell -a "tail -3  /etc/group"
全量添加
ansible  all  -m  user  -a  "groups=one   append=no  name=test"
ansible all -m  shell -a "tail -3  /etc/group"
参数:
 state=absent   删除用户   默认不删除家目录
 remove=yes   删除用户的同时删除家目录
[root@bb ~]# ansible  all  -m  user  -a  "state=absent remove=yes  name=test"
[root@bb ~]# ansible all -m shell -a "tail -1 /etc/passwd"
[root@bb ~]# ansible all -m shell -a "ls /home"
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值