QQ群:397745473
利用Ansible批量管理linux主机
常用的自动化运维工具:
Puppet: —基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
SaltStack: —基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YMAL,使得配置脚本更简单
Ansible: —基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2 模板语言,更强的远程命令执行操作
ansible简介:
Ansible 是一个简单的自动化运维管理工具,可以用来自动化部署应用、配置、编排 task(持续交付、无宕机更新等),采用 paramiko 协议库(fabric 也使用这个),通过 SSH 或者 ZeroMQ 等连接主机,大概每 2 个月发布一个主版本
简单的说:    让我们自动化部署APP;自动化管理配置项;自动化的持续交付;自动化的(AWS)云服务管理。 批量的在远程服务器上执行命令 。
Ansible则是提供了一套简单的流程,你要按照它的流程来做,就能轻松完成任务.
ansible的优点:
1、轻量级,他不需要去客户端安装agent,更新时,只需要在操作机上进行一次更新即可.
2、批量任务执行可以写成脚本,而且不用分发到远程就可以执行.
3、使用python编写的,维护更简单,ruby语法过于复杂 .
4、支持sudo.
利用Ansible批量管理linux主机
| 12
 3
 4
 5
 6
 
 | 参考: https://www.cnblogs.com/yeyou/p/5975900.html
 https://blog.51cto.com/u_13859004/2173972
 https://blog.51cto.com/tuwei/2388215
 
 使用Ansible批量管理远程服务器: https://www.linuxidc.com/Linux/2015-05/118080.htm
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 
 | 关闭iptables:
 service iptables stop
 chkconfig iptables off
 
 各个服务器节点的配置:
 hostname node1.server.com
 
 添加hosts解析
 [root@ansible ~]
 [root@ansible ~]
 192.168.1.63 node1.server.com
 192.168.1.64 node2.server.com
 192.168.1.20 ansible.server.com
 
 
 
 [root@centos01 ~]
 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:
 SHA256:cJz6NRTrvMDxX+Jpce6LRnWI3vVEl/zvARL7D10q9WY root@centos01
 The key's randomart image is:
 +---[RSA 2048]----+
 |          .   . .|
 |       . . +   oo|
 |      . = o o. oo|
 |       = * o..+ *|
 |      . S *.=+=*+|
 |       . o =+XooE|
 |        . ..=.++.|
 |           ..o ..|
 |           .. o. |
 +----[SHA256]-----+
 
 [root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub  root@192.168.100.20   <!--复制公钥到远端192.168.100.20-->
 [root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub  root@192.168.100.30    <!--复制公钥到远端192.168.100.30-->
 [root@centos01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1.server.com
 [root@centos01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2.server.com
 
 ssh-keygen #按照提示输入即可,一路回车也没问题,它会生成~/.ssh/id_rsa.pub 文件
 ssh-copy-id <远程主机> #这个会把上一步生成的pub文件放到远程主机,按提示输入ssh密码即可
 
 
 # 参考 : https://www.linuxidc.com/Linux/2020-01/162135.htm
 
 然后我们修改ansible配置文件 /etc/ansible/hosts
 [clients]
 代表一组主机的别名,这样就可以按组来操作。有些云平台象ec2 是需要用pem文件登录的。
 我们只要按第二台的格式写就了。
 ansible_user是登录用户名
 [clients]
 192.168.1.250
 111.111.111.111
 ansible_user=ubuntu
 ansible_ssh_private_key_file=~/my.pem
 
 
 Ansible简单测试
 ansible命令
 格式:ansible  节点名称   [ -m  模块]   -a  命令参数
 例:
 1、查看各个节点的时间
 [root@ansible ~]# ansible all -a date
 [root@ansible ~]# ansible all -m command -a date
 //以上两条命令结果是一样的,因为默认调用 command 模块,所以可以省略, all 表示所有节点, -a 后面是参数
 
 
 2、查看各个节点在线情况
 [root@ansible ~]# ansible all -m ping //内建的ping模块
 
 3、在各个节点输出指定信息
 [root@ansible ~]# ansible all -a "/bin/echo hello,world" #输出信息
 [root@ansible ~]# ansible all -a "/bin/df -h" #输出挂载信息
 [root@ansible ~]# ansible all -a "/sbin/ip addr show dev eth0 " #查看各节点的 eth0 网卡信息
 
 # Ansible-playbook
 1、Playbook是通过yaml文件来配置实现的,先来介绍下yaml语法!
 YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。\
 其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。YAML文件扩展名通常为.yaml,如example.yaml
 
 2、Playbook可以用来管理复杂任务
 对于需反复执行的、较为复杂的任务,我们可以通过定义 Playbook 来搞定。Playbook 是 Ansible 真正强大的地方,它允许使用变量、条件、循环、以及模板,也能通过角色 及包含指令来重用既有内容。下面我们来看看一些具体的实例。
 
 
 
 | 
案例1: 批量安装mysql-server软件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 
 | 1:批量安装mysql-server软件:[root@ansible ~]# vim mysql-server.yaml #建立mysql-server .yaml文件
 内容如下: (注意语法格式)
 选项解析:
 hosts:webserver         #指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组
 remote_user:root         #用于指定远程主机上的执行任务的用户
 tasks:                       #  任务
 -name:mysql-server installing    # 给这个任务起的名字
 yum:name=php           #利用yum模块,安装软件的包名为mysql-server
 state=present                 #状态为安装
 state=absent                  #状态为卸载
 
 [root@ansible ~]# ansible all -a "/bin/rpm -q mysql-server " # 检查node是否已经安装mysql-server
 [root@ansible ~]# ansible-playbook mysql-server.yaml
 看到结果,ok=2    changed=1 说明客户机上的mysql-server安装成功了!
 验证一下 mysql-server是否成功
 [root@ansible ~]# ansible all -a "/bin/rpm -q mysql-server "
 [root@ansible ~]# ansible all -a "/sbin/service mysqld start " #启动mysqld
 
 
 | 
案例2: 创建crontab计划
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | 1.建立cron.yaml,让每月10号来运行/root/backup.sh脚本[root@ansible ~]# vim crond.yaml
 内容如下:
 
 执行: [root@ansible ~]# ansible-playbook crond.yaml
 
 看到结果,ok=2    changed=1 说明客户机上的crontab计划创建成功了!
 
 [root@ansible ~]# ansible all -a 'crontab -l ' #查看各个节点crontab:
 注:
 "ansible-doc -l" 命令来查看它内置的有哪些模块。
 "ansible-doc  模块名"  命令来查看具体模块的详细用法。
 
 
 
 | 
使用ansible批量的添加用户
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 
 | 方法一:直接使用ansible命令[root@ansible ~]# openssl passwd -salt -l "abc123"
 -luVlrZexUGHM
 [root@ansible ~]# ansible all -m user -a "name=baigujing password=-luVlrZexUGHM shell=/bin/bash " -u root
 
 
 
 方法二:定义主机清单在使用ansible命令
 [root@ansible ~]# openssl passwd -salt -l "abc123"
 -luVlrZexUGHM
 [root@ansible ~]# vim hosts #添加主机地址
 [root@ansible ~]# ansible -i hosts webserver -m user -a "name=tester password=-luVlrZexUGHMshell=/bin/bash " -u root
 
 参数解释:
 -i #指定 inventory文件的位置;
 webserver #清单文件中的主机组名称
 -m #指定模块,不加此选项默认使用command模块
 user #添加用户的模块
 -a #编写模块内支持的指定
 -u #指定远程用户
 注意:密码不能是明文 注意是password 不是passwd
 .注意password必须是密文的,直接添加到/etc/shadow文件中
 
 
 
 创建用户组:
 ansible -i test puppet -m group -a "name=test state=present" -u root
 
 #建立用户组,使用的是新的模块 group
 删除用户:
 ansible -i hosts webserver -m user -a "name=tester remove=yes state=absent" -u root
 
 方法三:编写yaml文件
 1,生成密钥
 [root@ansible ~]# openssl passwd -salt -l "abc123"
 -luVlrZexUGHM #生成salt密钥
 
 
 2.编辑yaml文件
 [root@ansible ~]# vim useradd.yaml #编辑文件
 [root@ansible ~]# cat useradd.yaml #查看内容
 - hosts: webserver #定义主机组
 user: root #远程链接用户
 vars:
 user: jerry #添加的用户名
 tasks: #任务
 - name: add user #任务名称
 action: user name={{ user }} password=-luVlrZexUGHM shell=/bin/bash home=/home/{{ user }} #指定用户相关的信息 密码 必须是密文salt加密 登录shell 宿主目录
 
 
 
 3.执行yaml文件
 [root@ansible ~]# ansible-playbook useradd.yaml 成功
 
 
 
 
 | 
QQ群:397745473