(1)是基于python语言开发的自动化软件工具(2)是基于SSH远程管理服务实现远程主机批量管理(3)并行管理,部署简单,应用也简单方便
(1)提高工作的效率(2)提高工作的准确度(3)减少维护的成本(4)减少重复性工作
(1)可以实现批量系统操作配置(2)可以实现批量软件服务部署(3)可以实现批量文件数据分发(4)可以实现批量系统信息收集
(1)管理端不需要启动服务程序(no server)(2)管理端不需要编写配置文件(/etc/ansible/ansible.cfg)(3)受控端不需要安装软件程序(libselinux-python)(4)受控端不需要启动服务程序(no agent=无代理)(5)服务程序管理操作模块众多(module)(6)利用剧本编写来实现自动化(playbook)
(1)连接插件(connectior plugins) :用于连接主机 用来连接被管理端(2)核心模块(core modules) :连接主机实现操作, 它依赖于具体的模块来做具体的事情(3)自定义模块(custom modules) :根据自己的需求编写具体的模块(4)插件(plugins) :完成模块功能的补充(5)剧本(playbooks):ansible的配置文件,将多个任务定义在剧本中,由ansible自动执行(6)主机清单(host inventory):主机清单配置信息,定义ansible需要操作主机的范围(7)最重要的一点是 ansible是模块化的,它所有的操作都依赖于模块
系统、内核版本
[root@m01 ~]#cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@m01 ~]#uname -a Linux m01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux安装ansible
[root@m01 ~]yum repolist # 查看是否有相应epel源,安装ansible需要epel源 [root@m01 ~]yum install -y ansible [root@m01 ~]#rpm -qa ansible ansible-2.8.1-1.el7.noarch # ansible版本ansible 管理主机信息 -m 模块名称 -a "操作动作"
官方资料: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html配置文件
[root@m01 ~]#rpm -ql ansible /etc/ansible/ansible.cfg --- ansible配置文件 /etc/ansible/hosts --- 主机清单文件,定义管理的主机信息 /etc/ansible/roles --- 角色目录(更加规范使用ansible)ansible配置文件
vi /etc/ansible/ansible.cfg #ansible_become=True --- 开启用户切换功能 #ansible_become_method=sudo --- 使用什么方式切换用户权限 su / sudo #ansible_become_user=root --- 默认切换成什么用户 #ansible_become_ask_pass=false --- 是否使用密码认证命令检测所有IP地址可以用 all 来代替
root@m01 ~]#ansible all -m command -a "hostname" 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup 172.16.1.31 | CHANGED | rc=0 >> nfs01也可以用逗号隔开IP地址
[root@m01 ~]#ansible 172.16.1.31,172.16.1.41,172.16.1.7 -m command -a "hostname" 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.31 | CHANGED | rc=0 >> nfs01 172.16.1.41 | CHANGED | rc=0 >> backup编写服务内置变量信息
ansible_port=xxx --- 指定远程服务端口号 ansible_password=xxx --- 指定远程服务密码信息 ansible_user=xxx --- 指定以什么用户远程连接主机 ansible_host=xxx --- 指定远程主机地址信息和名称做映射,XXX=ip地址命令检测:在这里密码,端口都默认一样的
[root@m01 ~]#vim /etc/ansible/hosts nfs01 ansible_host=172.16.1.31 172.16.1.41 ansible_user=root ansible_password=123456 172.16.1.7 ansible_user=root ansible_password=123456 ansible_port=22 # 命令执行 [root@m01 ~]#ansible all -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup如果端口,密码不一样该怎么用
[root@m01 ~]#vim /etc/ansible/hosts nfs01 ansible_host=172.16.1.31 172.16.1.41 ansible_user=root ansible_password=123456 172.16.1.7 ansible_user=root ansible_password=654321 ansible_port=52113 # 命令执行 [root@m01 ~]#ansible all -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup编写服务主机组信息
[root@m01 ~]#vi /etc/ansible/hosts [ichn] nfs01 ansible_host=172.16.1.31 172.16.1.41 ansible_user=root ansible_password=123456 [web] 172.16.1.7 ansible_user=root ansible_password=123456 ansible_port=22命令检测主机组所有名称可以用 all 代替
[root@m01 ~]#ansible all -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup主机组所有名称之间可以用逗号隔开
[root@m01 ~]#ansible ichn,web -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup编写服务主机子组信息children:是固定的
[root@m01 ~]#vim /etc/ansible/hosts [nfs:children] nfs_server nfs_client [nfs_server] nfs01 ansible_host=172.16.1.31 [nfs_client] 172.16.1.41 ansible_user=root ansible_password=123456 172.16.1.7 ansible_user=root ansible_password=12345 ansible_port=22命令检测所有主机组,子组都可以用 all 来代替
[root@m01 ~]#ansible all -m command -a "hostname" 172.16.1.41 | CHANGED | rc=0 >> backup 172.16.1.7 | CHANGED | rc=0 >> web01 nfs01 | CHANGED | rc=0 >> nfs01所有主机组,子组都可以用 nfs 来代替
[root@m01 ~]#ansible nfs -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01 172.16.1.7 | CHANGED | rc=0 >> web01 172.16.1.41 | CHANGED | rc=0 >> backup可以单独启动其中一个子组
[root@m01 ~]#ansible nfs_server -m command -a "hostname" nfs01 | CHANGED | rc=0 >> nfs01配置主机清单变量信息
[root@m01 ~]#vim /etc/ansible/hosts [nfs:vars] pass=123456 port=873 [nfs] 172.16.1.31 172.16.1.41 [nfs_client] 172.16.1.41 [nfs_client:vars] ansible_user=root ansible_password=123456命令检测所有主机清单变量都可以用 all 来代替
[root@m01 ~]#ansible all -m command -a "hostname" 172.16.1.31 | CHANGED | rc=0 >> nfs01 172.16.1.41 | CHANGED | rc=0 >> backup可以用变量的主机组来代替
[root@m01 ~]#ansible nfs -m command -a "hostname" 172.16.1.31 | CHANGED | rc=0 >> nfs01 172.16.1.41 | CHANGED | rc=0 >> backup(1)查看主机清单配置文件是否正确 (2)利用SSH命令直接连接,连接不上查看有没有公钥 密码正不正确 指定用户正不正确 防火墙是否阻止(3)远程管理一直报错,只要老的ansible远程会话存在
[root@m01 /]#ps -ef|grep ssh # SSH远程服务进程(实现客户端远程连接) root 1211 1 0 09:49 ? 00:00:00 /usr/sbin/sshd -D # SSH远程连接会话进程(维持连接会话) root 2179 1211 0 15:16 ? 00:00:00 sshd: root@pts/1,pts/2command – Execute commands on targets:对目标主机执行命令Synopsis:模块详细说明(1)模块多个参数要用空格分隔(2)使用commad模块一些特殊符号信息不能使用,如果非要执行请使用shell模块像变量$HOME和"<",">","|",";"和"&"将不起作用Parameters:模块的参数
chdir在运行命令之前,先切换指定目录(远程主机)creates在执行命令前,判断远程主机指定文件是否存在,如果存在,命令不执行removes在执行命令前,判断远程主机指定文件是否存在,如果存在,命令继续执行free_form在使用这个模块是, -a后面必须输入一个合法linux命令参数演示chdir:在运行命令之前,先切换指定目录(远程主机)
[root@m01 ~]#ansible 172.16.1.41 -m command -a "pwd" 172.16.1.41 | CHANGED | rc=0 >> /root [root@m01 ~]#ansible 172.16.1.41 -m command -a "chdir=/tmp pwd" 172.16.1.41 | CHANGED | rc=0 >> /tmpcreates:在执行命令前,判断远程主机指定文件是否存在,如果存在,命令不执行
[root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf touch /etc/rsyncd.conf" 172.16.1.41 | SUCCESS | rc=0 >> skipped, since /etc/rsyncd.conf exists # 文件已存在,不会执行后续命令 [root@m01 ~]#ansible 172.16.1.41 -m command -a "creates=/tmp/test.txt touch /tmp/test.txt" [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. 172.16.1.41 | CHANGED | rc=0 >> # 文件没有,执行后续命令创建文件 [root@backup tmp]#ll total 2 -rw-r--r-- 1 root root 0 Jul 24 09:52 test.txtshell – Execute shell commands on targets:在远程目录主机执行命令Parameters:模块的参数 和command参数一样
chdir在运行命令之前,先切换指定目录(远程主机)creates在执行命令前,判断远程主机指定文件是否存在,如果存在,命令不执行removes在执行命令前,判断远程主机指定文件是否存在,如果存在,命令继续执行free_form在使用这个模块是, -a后面必须输入一个合法linux命令参数演示shell识别 >
[root@m01 ~]#ansible 172.16.1.41 -m shell -a "echo 123456 >/tmp/test.txt" 172.16.1.41 | CHANGED | rc=0 >> [root@backup tmp]#cat test.txt 123456Runs a local script on a remote node after transferring it:远程批量执行本地脚本信息shell模块远程执行脚本第一步:编写一个脚本
[root@m01 scripts]#vim yum.sh #!/bin/bash yum install -y htop第二步:远程传输脚本
[root@m01 scripts]#ansible 172.16.1.41 -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts"第三步:批量执行脚本文件
[root@m01 scripts]#ansible 172.16.1.41 -m shell -a "sh /server/scripts/yum.sh"script 远程执行脚本第一步:编写脚本
[root@m01 scripts]#vim yum.sh #!/bin/bash yum install -y htop第二步:远程执行脚本
[root@m01 scripts]#ansible 172.16.1.41 -m script -a "/server/scripts/yum.sh" # scripy模块可以省略sh相比shell模块来讲,script不用将脚本传输到远程主机,脚本本身不用进行授权,即可利用script模块执行,不需要使用sh,更加简洁,高效。
copy:可以将本地数据批量拷贝到远程主机,可以将远程主机数据进行移动操作(并不是拉取)
src 指定本地要推送的源数据信息dest指定保存数据目录路径信息mode数据推送后修改数据权限owner修改数据推送后的所属用户信息group修改数据推送后的所属组信息remote_src指定源为远程主机路径信息backup将数据进行备份content在指定远程主机生成有数据的文件,指定文件内容directory_mode递归设定目录的权限,默认为系统默认权限forces如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖。如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes。others所有的file模块里的选项都可以在这里使用使用copy 模块,将/ichn/test.txt文件传输到各个服务器,权限修改为666 属主属组为ichn
[root@m01 ichn]#ansible all -m copy -a "src=/ichn/test.txt dest=/backup mode=666 owner=ichn group=ichn"检查结果
[root@m01 ichn]#ansible all -m shell -a "ls -l /backup/test.txt" 172.16.1.41 | CHANGED | rc=0 >> -rw-rw-rw- 1 ichn ichn 0 Jul 20 14:48 /backup/test.txt 172.16.1.31 | CHANGED | rc=0 >> -rw-rw-rw- 1 ichn ichn 0 Jul 24 11:05 /backup/test.txtansible all -m copy -a "src=/ichn/ =/backup mode=666 owner=ichn group=ichn"ansible all -m copy -a "src=/ichn dest=/backup mode=666 owner=ichn group=ichn"说明: 复制目录是遵循rsync复制目录原理 目录后面有/ 和 没有/有区别实现数据备份
ansible nfs -m copy -a "src=/ichn/ichn.txt dest=/backup backup=yes"批量备份:
ansible nfs -m copy -a "src=/backup/ichn.txt dest=/backup/ichn.txt.bak remote_src=yes"批量还原:
ansible nfs -m copy -a "src=/backup/ichn.txt.bak dest=/backup/ichn.txt remote_src=yes"生成一个有数据的文件
ansible nfs_client -m copy -a "content="ichn123" dest=/etc/rsync.password mode=600"参数:
dest将远程主机拉取过来的文件保存在本地的路径信息src指定从远程主机要拉取的文件信息,只能拉取文件flat默认设置为no,如果设置为yes,将不显示172.16.1.41/etc/信息参数演示:dest,src
[root@m01 tmp]#ansible 172.16.1.41 -m fetch -a "src=/etc/hosts dest=/tmp" [root@m01 tmp]#ll total 0 drwxr-xr-x 3 root root 17 Jul 24 21:27 172.16.1.41 drwx------ 2 root root 6 Jul 23 08:20 vmware-rootflat=yes
[root@m01 tmp]#ansible 172.16.1.41 -m fetch -a "src=/etc/hosts dest=/tmp/ flat=yes" [root@m01 tmp]#ls hosts vmware-root参数:
path指定远程主机上已有的一个文件数据mode修改数据权限数值owner修改属主group修改属组src指定要创建软链接的文件信息recurserecurse=yes 将目录及目录里文件的权限递归修改 recurse=no 不递归statestate=directory创建目录,可以递归创建目录state=touch创建文件:如果路径不存在将创建一个空文件state=link创建软链接state=hard创建硬链接state=absent删除数据state=file检查文件是否存在参数演示:mode owner group
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/test.txt mode=666 owner=ichn group=ichn" [root@backup tmp]#ll -d test.txt -rw-rw-rw- 1 ichn ichn 7 Jul 24 10:07 test.txtstate=directory:创建目录
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/yang state=directory" [root@backup tmp]#ll total 8 drwxr-xr-x 2 root root 6 Jul 24 21:52 yang递归创建目录
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/yang/yang1/yang2 state=directory" [root@backup tmp]#tree yang yang └── yang1 └── yang2 2 directories, 0 filesstate=touch:创建文件,如果路径不存在将创建一个空文件
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/ceshi.txt state=touch"state=link:创建软链接
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "src=/tmp/test.txt path=/backup/test_soft_link.txt state=link"state=hard:创建硬链接
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "src=/tmp/test.txt path=/backup/test_hard_link.txt state=hard"state=absent:删除数据操作
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/backup/test_hard_link.txt state=absent"state=file:检查文件是否存在
[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/ceshi.txt state=file"recurse=yes:递归修改权限
[root@m01 ~]#ansible 172.16.1.41 -m file -a "path=/tmp/yang/ mode=700 owner=ichn group=ichn recurse=yes"参数:
name 指定要安装的软件名称statestate=installed安装软件state=presentstate=latest更新软件state=removed移除卸载软件state=absent参数演示:absent installed removed
[root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=absent" --- 卸载软件 [root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=installed" --- 安装软件 [root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=removed" --- 卸载软件参数
name管理哪个服务名称statestate=reloaded平滑重启服务state=restarted重启服务state=started启动服务state=stopped停止服务enabledenabled=no开机不运行enabled=yes开机自动运行参数演示:restarted started stopped enabled=no enabled=yes
[root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=stopped" 停止服务 [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=started" 启动服务 [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=restarted" 重启服务 [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond enabled=no" 开机不启动服务 [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond enabled=yes" 开机启动服务参数:
minute 分Minute when the job should run ( 0-59, , /2, etc )hour 时Hour when the job should run ( 0-23, , /2, etc )day 日Day of the month the job should run ( 1-31, , /2, etc )month 月Month of the year the job should run ( 1-12, , /2, etc )weekday 周Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )job 工作:要做的事情name 定义定时任务的描述信息disableddisabled=no 取消注释disabled=yes 添加注释statestate=absent删除定时任务state=present创建定时任务要求: 每隔五分钟,进行时间同步
ansible 172.16.1.41 -m cron -a "minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"要求: 每周五,晚上10:30, 需要去大保健
ansible 172.16.1.41 -m cron -a "name='大保健' minute=30 hour=22 weekday=5 job='make shufu &>/dev/null'" 说明: 定时任务配置时.需要添加name注释信息,否则会出现反复创建相同定时任务注释定时任务 删除定时任务 创建定时任务
ansible 172.16.1.41 -m cron -a "name='大保健03' state=absent" --- 删除指定定时任务 ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=yes" ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=no"参数
name创建的用户名称password设置用户密码信息 必须设置为密文create_homeyes 表示创建家目录 no 不创建家目录shell指定用户登录方式 shell=/sbin/nologingroup指定用户属于哪个组 主要组groups指定用户属于哪个组 附属组uid指定用户uid数值state=absent删除用户remove=yes当与state=absent连用的时候,彻底删除指定用户。remove=no 不删除家目录参数演示:name create_home=no shell=/sbin/nologin group groups uid state=absent
# 批量创建虚拟用户 ansible 172.16.1.41 -m user -a "name=alex create_home=no shell=/sbin/nologin" # 指定用户所属组 ansible 172.16.1.41 -m user -a "name=alex group=ichn groups=oldgirl" # 指定用户uid ansible 172.16.1.41 -m user -a "name=alex uid=2000" # 删除用户 ansible 172.16.1.41 -m user -a "name=alex state=absent"彻底删除指定用户:state=absent remove=yes
[root@m01 ~]#ansible 172.16.1.41 -m user -a "name=alex group=alex state=absent remove=yes"给用户设置密码
ansible 172.16.1.41 -m user -a 'name=oldbaby password="$6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1"'密码生成方式方法一:明文加密,算法生成密文
ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}" # mypassword --- 明文密码信息 # sha512 --- 明文转换为密文加密方法 # mysecretsalt --- 用什么做算法依据生成密文信息实践演示
[root@m01 ~]#ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'ichn123') }}" localhost | SUCCESS => { "msg": "$6$ichn123$W3jkmkkVTr.9UStm4S50RT2uIEjB/4GEtaAeVCSZ..uWVN1YGxHvluss9JVfAPV0gSJoGn1qAfxGyttIsTjcz0" }方法二:centos7无法使用mkpasswd --method=sha-512方法三:利用python模块功能直接安装
[root@m01 ~]#yum install -y python-pip优化pip源
[root@m01 ~]#mkdir ~/.pip/ [root@m01 ~]#vim ~/.pip/pip.conf [global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))" [root@m01 ~]#pip install passlib # 安装参数演示:src path fstype=nfs state=mounted state=unmounted
mount -t nfs 172.16.1.31:/data /mnt # /mnt挂载到172.16.1.31:/data # 立即挂载 并且添加在/etc/fstab文件 ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted" # 立即卸载,/etc/fstab文件中不会永久卸载 ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=unmounted"哪些是重要参数(1)通过其他人博文(2)看官网网站文档中,有红色字体标记的(3)看官网网站文档中,举例配置中参数使用频次 ansible帮助信息查看方法(1)查看ansible所有模块:ansible-doc -l(2)查看ansible模块参数信息:ansible-doc -s cron(模块可以换)(3)查看ansible模块详细信息:ansible-doc cron(模块可以换) ansible颜色提示信息说明
黄色对远程主机做出了改动绿色查询信息,对远程主机没有做改动红色远程执行命令出错了紫色警告信息(建议信息)蓝色命令执行的过程转载于:https://www.cnblogs.com/basa/p/11298916.html