自动化运维工具-Ansible

#一、Ansible介绍
###1. 自动化运维工具对比

    1. Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
    1. SaltStack:基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YAML,使得配置脚本更简单.需要配置客户端以及服务器端。每台被控制节点需要安装agent
    1. Ansible:基于 Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作

###2. Ansible简介
ansible是新出现的自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

Ansible特性:

  • no agents:不需要在被管控主机上安装任何客户端,更新时,只需在操作机上进行一次更新即可(不用安装客户端。分布式的)
  • no server:无服务器端,使用时直接运行命令即可
  • modules in any languages:基于模块工作,可使用任意语言开发模块
  • yaml,not code:使用yaml语言定制剧本playbook
  • ssh by default:基于SSH工作
  • strong multi-tier solution:可实现多级指挥

image.png

图片解释:

  • connection plugins:连接插件,负责和被监控端实现通信,默认使用SSH连接
  • host inventory:主机清单,是一个配置文件里面定义监控的主机
  • modules : 模块,核心模块、command模块、自定义模块等
  • plugins : modules功能的补充,包括连接插件,邮件插件等
  • playbook:编排,定义 Ansible 多任务配置文件,非必需

#二、Ansible安装
###1. 准备环境----关闭防护墙和selinux

环境:
主机:4台  一个控制节点 3个被控制节点
解析:本地互相解析(所有机器)
# vim /etc/hosts
192.168.181.128 ansible-web1
192.168.181.129 ansible-web2
192.168.181.130 ansible-web3
192.168.181.146 ansible-server  (控制节点服务器端)
配置ssh公钥认证:控制节点需要发送ssh公钥给所有被控制节点
[root@ansible-server ~]# ssh-keygen

[root@ansible-server ~]# ssh-copy-id -i ansible-web1     #所有被控机器

[root@ansible-server ~]# ssh ansible-web1     #手动连接一遍所有被控机器

所有机器:
systemctl stop firewalld && setenforce 0

###2. 控制节点安装ansible

 1. 配置EPEL网络yum源(直接第二步 没有ansible再执行这一步)
 [root@ansible-server ~]# yum install -y epel-release
 2. 安装ansible
 [root@ansible-server ~]# yum install -y ansible
 3.查看版本
 [root@ansiable-server ~]# ansible --version
 4.查看帮助
 [root@ansible-server ~]# ansible --help

###3. ansible基础----inventory主机清单
inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、密码以及key相关信息。
官方文档

#查看配置文件:
[root@ansible-server ~]# rpm  -qc ansible       #-q:---query查询
/etc/ansible/ansible.cfg       #主要设置一些ansible初始化的信息,比如日志存放路径、模块、等配置信息
/etc/ansible/hosts             #被控服务器的信息,默认位置为/etc/ansible/hosts

#添加主机或者主机组:
[root@ansible-server ~]# vim /etc/ansible/hosts    #在最后追加被管理端的机器
[ansible-webs]
ansible-web1
ansible-web2
ansible-web3

-------------------------------例----------------------------------
#添加主机
ansible-web1          #可以使用主机名称或IP地址

#添加主机组:
[webservers]      #使用[]标签指定主机组 ----标签自定义
192.168.181.129     #如果未解析添加ip
ansible-web2      #解析添加主机名

#组可以包含其他组:
[webservers1]     #组一
ansible-web1
[webservers2]     #组二
ansible-web2
[weball:children]      #weball组包括两个子组,children--照写
webservers1        #组一
webservers2        #组二
------------------------------------------------------------------

#为一个组指定变量,组内每个主机都可以使用该变量:
[ansible-webs:vars]          #设置ansible-webs组变量,vars--照写
ansible_ssh_port=22          #端口
ansible_ssh_user=root        #远程用户
ansible_ssh_private_key_file=/root/.ssh/id_rsa      #私钥位置
#ansible_ssh_pass=1234          #也可以定义密码,如果没有互传秘钥可以使用密码。
Ansible Inventory 常见的内置参数:

image.png

#####查看组内主机列表:

语法:ansible 组名 --list-hosts

[root@ansible-server ~]# ansible ansible-webs --list-hosts
 hosts (3):
   ansible-web1
   ansible-web2
   ansible-web3

#####扩展:自定义主机列表使用密码登录:

[root@ansible-server ~]# vim /opt/hostlist
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root    #被控服务器账号
#ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_pass=test    #被控服务器密码

[all]
ansible-web1
ansible-web2
ansible-web3

#使用:
[root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
小注释:如果不通,手动连接第一次,第一次需要手动输入密码。"第一次"
-i:指定清单文件
注意:这里的ping并不是真正意义上的ping而是探测远程主机ssh是否可以连接!判断ssh端口是否存活

###4. 测试

语法:
ansible <pattern> -m <module_name> -a <arguments>
pattern:主机清单里定义的主机组名,主机名,IP,别名等,all表示所有的主机,支持通配符,正则
-m module_name:模块名称,默认为command
-a arguments:传递给模块的参数
-o :横着显示(单行显示)

#####示例:

#使用ping模块检查ansible节点的连通性:
#指定单台机器
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}

#指定多台机器
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"changed": false, "ping": "pong"}

#指定组
[root@ansible-server ~]# ansible ansible-webs -m ping -o
ansible-web2 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web3 | SUCCESS => {"changed": false, "ping": "pong"}
-------------------------------------------------------------------------------------------

#执行shell命令:
[root@ansible-server ~]# ansible ansible-web1 -m shell -a 'uptime' -o
ansible-web1 | SUCCESS | rc=0 | (stdout)  21:28:25 up  2:53,  3 users,  load average: 0.16, 0.05, 0.06

#不加 -m  默认是 command 模块
[root@ansible-server ~]# ansible ansible-web1 -a 'uptime' -o
ansible-web1 | SUCCESS | rc=0 | (stdout)  21:29:07 up  2:54,  3 users,  load average: 0.08, 0.04, 0.05

#给节点增加用户:
[root@ansible-server ~]# ansible ansible-web1 -a 'useradd tom'
ansible-web1 | SUCCESS | rc=0 | (stdout)

[root@ansible-web1 ~]# id tom
uid=1000(tom) gid=1000(tom) 组=1000(tom)

[root@ansible-server ~]# ansible ansible-web1 -a 'grep tom /etc/passwd' -o
ansible-web1 | SUCCESS | rc=0 | (stdout) tom:x:1000:1000::/home/tom:/bin/bash

#重定向输出到本地文件中:
[root@ansible-server ~]# ansible ansible-web1 -a 'df -Th' > a.txt

[root@ansible-server ~]# cat a.txt 
ansible-web1 | SUCCESS | rc=0 >>
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs       8.0G  2.2G  5.9G   27% /
devtmpfs                devtmpfs  226M     0  226M    0% /dev
tmpfs                   tmpfs     237M     0  237M    0% /dev/shm
tmpfs                   tmpfs     237M  4.7M  232M    2% /run
tmpfs                   tmpfs     237M     0  237M    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  125M  890M   13% /boot
tmpfs                   tmpfs      48M     0   48M    0% /run/user/0

###5. Ad-Hoc
ad hoc就是执行简单的命令 (一条命令) 。对于复杂的命令则为 playbook。

列出ansible支持的模块:
-l:获取列表
-s module_name:获取指定模块的使用信息

#看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
[root@ansible-server ~]# ansible-doc -l

#查看模块使用信息,了解其功能:
[root@ansible-server ~]# ansible-doc -s yum

#####常用模块
#####1. 远程复制备份 copy 模块

模块参数详解:
src=:指定源文件路径
dest=:目标地址(拷贝到哪里)
owner=:指定属主
group=:指定属组
mode=:指定权限,可以以数字指定比如0644
backup=:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no

[root@ansible-server ~]# vim test.txt     #创建一个测试文件
[root@ansible-server ~]# cat test.txt 
test

#将 server 下 /root/test.txt 拷贝到 web2/root/test.txt 属主属组root 权限644
[root@ansible-server ~]# ansible ansible-web2 -m copy -a 'src=/root/test.txt dest=/root owner=root group=root mode=644' -o
ansible-web2 | SUCCESS => {"changed": false, "checksum": "016c49988e9afc97ab7fe8126867a986e20432f8", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/root/test.txt", "secontext": "system_u:object_r:admin_home_t:s0", "size": 11, "state": "file", "uid": 0}

[root@ansible-server ~]# vim test.txt      #追加如下内容
[root@ansible-server ~]# cat test.txt 
test
ceshi

#将源文件先备份在拷贝
[root@ansible-server ~]# ansible ansible-web2 -m copy -a 'src=/root/test.txt dest=/root owner=root group=root mode=644 backup=yes'
ansible-web2 | SUCCESS => {
    "backup_file": "/root/test.txt.12190.2020-03-19@16:34:42~", 
    "changed": true, 
    "checksum": "016c49988e9afc97ab7fe8126867a986e20432f8", 
    "dest": "/root/test.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b1b8d0a109a1ed187e314557b35655b2", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1584606879.85-10069951504721/source", 
    "state": "file", 
    "uid": 0
}

[root@ansible-web2 ~]# ls      #查看web2
anaconda-ks.cfg  test.txt  test.txt.12190.2020-03-19@16:34:42~
[root@ansible-web2 ~]# cat test.txt
test
ceshi
[root@ansible-web2 ~]# cat test.txt.12190.2020-03-19\@16\:34\:42~ 
test

注意:如果文件没有变化,不会备份。只有文件内容不同,才会做备份。

#移动被控制节点的文件
[root@ansible-server ~]# ansible ansible-web2 -a 'mv /root/test.txt /opt'
ansible-web2 | SUCCESS | rc=0 | (stdout)

[root@ansible-web2 ~]# cat /opt/test.txt 
test
ceshi

#####2. 软件包管理 yum 模块

模块参数详解:
state=:状态是什么,干什么
state=absent:remove安装包
state=latest:最新的(安装)
state=removed:卸载

#安装tree
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=latest"

#查看
[root@ansible-web3 ~]# tree
.
└── anaconda-ks.cfg

0 directories, 1 file

#卸载tree
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=removed"
或者
[root@ansible-server ~]# ansible ansible-web3 -m yum -a "name=tree state=absent"

#查看
[root@ansible-web3 ~]# tree
-bash: /usr/bin/tree: 没有那个文件或目录

#####3. 服务管理 service 模块

模块参数详解:
state=:状态是什么,干什么
state=started:启动
state=stopped:停止
state=restarted:重启
enabled=yes:开机自启
enabled=no:开机不自启

#启动nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=started"

#停止nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=stopped"

#重启nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx state=restarted"

#开机自启nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx enabled=yes"

#开机不自启nginx
[root@ansible-server ~]# ansible ansible-web3 -m service -a "name=nginx enabled=no"

#####4. 文件 file 模块

模块参数详解:
owner=:修改属主
group=:修改属组
mode=:修改权限
path=:要修改文件的路径
recurse=:递归的设置文件的属性,只对目录有效,两个选项:yes|no
state=touch:创建一个新的空文件
state=directory:创建一个新的目录,当目录存在时不会进行修改

#在 web1 的/root下创建 目录chen 权限777 属主属组chen
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen state=directory mode=777 owner=chen group=chen'

#在 web1 的/root/chen下创建 文件chen.txt 权限777 属主属组chen
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen/chen.txt state=touch mode=777 owner=chen group=chen'

#查看
[root@ansible-web1 ~]# ll
总用量 0
drwxrwxrwx. 2 chen chen 22 3月  20 00:33 chen
[root@ansible-web1 ~]# ll chen
总用量 0
-rwxrwxrwx. 1 chen chen 0 3月  20 00:33 chen.txt

#递归更改 web1 /root/chen目录 权限755 属主属组root
[root@ansible-server ~]# ansible ansible-web1 -m file -a 'path=/root/chen owner=root group=root mode=755 state=directory rec
urse=yes'

#查看
[root@ansible-web1 ~]# ll
总用量 0
drwxr-xr-x. 2 root root 22 3月  20 00:33 chen
[root@ansible-web1 ~]# ll chen
总用量 0
-rwxr-xr-x. 1 root root 0 3月  20 00:33 chen.txt

#####5. 收集信息 setup 模块

模块参数详解:
filter=:过滤信息

#收集所有信息
[root@ansible-server ~]# ansible ansible-web1 -m setup

#收集 web1 的ip
[root@ansible-server ~]# ansible ansible-web1 -m setup -a 'filter=ansible_all_ipv4_addresses'
ansible-web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.181.128"
        ]
    }, 
    "changed": false
}
发布了92 篇原创文章 · 获赞 0 · 访问量 1433

猜你喜欢

转载自blog.csdn.net/Forgetfanhua/article/details/105249493