ansible远程自动化运维、常用模块详解

一、ansible是基于python开发的配置管理和应用部署工具;也是自动化运维的重要工具;可以批量配置、部署、管理上千台主机;只需要在一台主机配置ansible就可以完成其它主机的操作。

1.操作模式:

模块化操作,命令行执行

playbook、剧本、也是把命令行脚本化,脚本的模式就是yaml格式

2.ansible的特性:幂等性(指的是多次操作或者是多次执行,对系统的影响不会发生变化,无论执行多少次结果都是一样的,但restart不同,因为是重启,每一次都要关闭和开机);

不可以进行远程主机的动态操作

3.ansible的四大组件:

1>inventory:主机清单(管理机器必须要声明管理主机的地址和其它配置,不声明的话就无法操作目标主机)

2>modules:模块,ansible的功能都是由模块来实现的

3>插件

4>playbooks:剧本----脚本,作用就是复用

二、模块和语法的学习:

1.安装ansible

关闭防火墙及安全机制

安装epel源

安装ansible

进入ansible目录内

声明管理主机的IP

设置密钥对

密钥免密登录

ansible-doc -l :列出ansible已安装的模块

2.command #基础模块,也是ansible默认模块,不支持管道符和重定向操作 执行一般的linux命令

ansible <组名或者ip地址> -m指定模块,不加-m默认使用command -a 《 参数或者命令》

ansible 192.168.127.22 -m command -a "date"

ansible all

不加-m默认就是command模块

出错处理方式:重新进行远程主机密钥免密登录

解压:一定要指定路径

command常用的三个参数

chdir 在目标主机提前进入目录然后执行指令

creates #判断文件是否存在,如果存在就不执行后面的命令,不存在则执行后面的命令

removes 判断文件是否存在,如果存在则执行指令,不存在不执行

3.# shell模块 支持管道符和重定向,也可以用逻辑或表达式 &&表示且 ;表示或

管道符

重定向

逻辑且 &&

逻辑或 ;

习题: #目标主机创建一个脚本,在脚本中写/bin/bash ifconfig然后运行脚本,在一条命令执行

##\n表示换行执行

4.cron模块:定时任务模块 minute/hour/day/month/weekday 分/时/日/月/周

**job:**任务计划需要执行的命令或者脚本

**name:**任务计划的名称,可以不加

创建定时任务:任务名为xy1 每11月11号11点11分,执行一次ls /opt 任务

创建定时任务:每五分钟执行一次

查看定时任务:

删除定时任务

5.USER模块:用户管理模块

常用的参数:

home 指定家目录

name 指定用户名,必须要有

state present:创建用户,可以不加,默认

absent :删除一定要加absent

system=yes|no。标记用户使用是一个程序用户|普通用户(yes为程序用户、no为普通用户)

uid:用户的唯一标识

group:指定用户的所在组

create_home=yes|no:替换用户的家目录,不替换可以不写,可省略

password:给用户创建密码

comment:添加用户的注释信息,可有可无

remove=yes|no,当删除用户时,加上remove=yes,删除用户的家目录,相当于userdel -r。如果不需要删除用户家目录,可省略这一项

如果创建程序用户,一定要声明system=yes,声明shell的类型 shell=/sbin/nologin

创建一个程序用户 名字为chendong 指定shell为sbin/nologin

创建用户liudong,选择uid800(uid800为普通用户),创建密码123456,指定家目录 /opt/test1:

远程主机cat /etc/passwd查看

更改用户家目录:

删除用户及其家目录

6.ping模块:

ping所有远程主机

7.hostname模块:更改远程的主机名

8.copy模块:用于复制指定的主机文件到远程主机的模块

常用的参数:

dest:指出要复制的文件在哪,必须使用绝对路径。如果源目标是目录,指目标也得是目录。如果目标的文件存在,会覆盖原有的内容

src:复制远程主机的源文件,最好使用绝对路径,源目标是目录,指目标也得是目录

owner:指定文件的所有者

group:指定文件的所在组

content:从主机复制指定的内容到目标主机,用content就不能使用src

mode:指定复制之后的文件的权限

将本机的opt目录下123文件复制到远程主机的opt目录下的123.txt里,并把所有者及所在者指定为test 权限777

iptables模块

filewalld模块:指定端口和服务类型是一样的

删除

配置网卡

修改IP

script模块:运行本地脚本,把脚本运行的结果输出到远程主机

setup模块:查看远程主机的信息IP、cpu、内核、系统信息

查看cpu

查看内核版本一定要会看

查看内存

查看系统信息

三、主机清单 vim /etc/ansible/hosts

写在后面只对指定40远程主机有效

写在下面对整个组都有效

定义多个主机[1:9][0:9]:批量匹配IP地址

以组进行嵌套访问

Ansible

Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。 它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,Ansible基本上都可以实现。

Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作, 使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。

Ansible是基于模块工作的,它只是提供了一种运行框架,它本身没有完成任务的能力, 真正执行操作的是Ansible的模块, 比如copy模块用于拷贝文件到远程主机上, service模块用于管理服务的启动、停止、重启等。

Ansible其中一个比较鲜明的特性是Agentless,即无Agent的存在,它就像普通命令一样,并非C/S软件, 也只需在某个作为控制节点的主机上安装一次Ansible即可,通常它基于ssh连接来控制远程主机, 远程主机上不需要安装Ansible或其它额外的服务。

使用者在使用时,在服务器终端输入命令或者playbooks,会通过预定好的规则将playbook拆解为play, 再组织成ansible可以识别的任务,调用模块和插件, 根据主机清单通过SSH将临时文件发给远程的客户端执行并返回结果, 执行结束后自动删除

Ansible的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。 所谓幂等性,指的是多次操作或多次执行对系统资源的影响是一致的。 比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态, 它什么也不会做,所以多次停止的结果仍然是停止,不会改变结果, 它是幂等的,而 systemctl restart xxx 是非幂等的。

幂等性是计算机科学中的一个概念,用于描述一个操作或请求的性质。 在幂等操作中,无论执行多少次,结果都是一样的。 简而言之,多次执行相同的操作,不会产生不同的效果。

对于 HTTP 请求来说,幂等性通常用于描述对同一资源的多次请求是否会导致状态的变化。 一个幂等的 HTTP 方法是指,如果你对同一资源进行多次相同的请求, 产生的结果与执行一次请求的效果是相同的。

例如,HTTP 的 GET 方法就是幂等的,因为多次请求同一资源的 GET 操作不会改变资源的状态。 而 POST 方法通常不是幂等的,因为多次执行相同的 POST 请求可能会创建多个相同的资源,导致状态的变化。

Ansible的很多模块在执行时都会先判断目标节点是否要执行任务,所以, 可以放心大胆地让Ansible去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。

Ansible的四大组件: Inventory 主机清单(主机组) Modules 模块 Plugins 插件 Playbooks 剧本(相当于脚本)

Ansible的优缺点

优点 部署较为简单, 只需要在控制主机上部署ansible环境,被控制端上只要求安装ssh和python 2.5以上版本, 对于运维人员使用门槛低。

被管控节点无需安装Agent。 无服务端,使用是直接调用控制端命令或者脚本。

基于模块工作, 可以使用任意语言开发模块。

基于yaml语法编写playbook。

定义的任务已存在则不会做任何事情,意味着在同一台服务器上多次执行同一个playbook是安全的。

缺点 学习成本:需要学习Ansible自定义的脚本语法规则。

安装成本:控制主机需是一台非Windows远程主机,也就是说至少要有1台远程Server。

易用性:因为必须有远程主控机,若本地有文件需同步,则需先将文件传输到对应的主控机, 才能够做分发同步。

---------- ansible 环境安装部署 ---------- 管理端:192.168.233.10 ansible 被管理端:192.168.233.20 被管理端:192.168.233.30

//管理端安装 ansible yum install -y epel-release //先安装 epel 源 yum install -y ansible

//ansible 目录结构 /etc/ansible/ ├── ansible.cfg #ansible的配置文件,一般无需修改 ├── hosts #ansible的主机清单,用于存储需要管理的远程主机的相关信息 └── roles/ #公共角色目录

//配置主机清单 cd /etc/ansible vim hosts [webservers] #配置组名 192.168.233.20 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)

dbservers\] 192.168.233.30 #这里的配置组,只是分组,在实际使用中,你可以根据你的环境、应用程序组件的角色等来创建不同的组。 例如,webservers 可能包含所有 web 服务器,而 dbservers 可能包含所有数据库服务器。 //配置密钥对验证 ssh-keygen -t rsa #一路回车,使用免密登录 sshpass -p '123' ssh-copy-id root@192.168.233.20 sshpass -p '123' ssh-copy-id root@192.168.233.30 --------- ansible 命令行模块 --------- 命令格式:ansible \<组名/ip\> -m \<模块\> -a \<参数列表\> ansible-doc -l #列出所有已安装的模块,按q退出 1.command 模块 //在远程主机执行命令,不支持管道,重定向等shell的特性。 ansible-doc -s command #-s 列出指定模块的描述信息和操作动作 ansible 192.168.233.30 -m command -a 'date' #指定 ip 执行 date ansible webservers -m command -a 'date' #指定组执行 date ansible dbservers -m command -a 'date' ansible all -m command -a 'date' #all 代表所有 hosts 主机 ansible all -a 'ls /' #如省略 -m 模块,则默认运行 command 模块 ansible 192.168.233.20 -a 'chdir=/opt tar -xf nginx-1.22.0.tar.gz' ansible 192.168.233.20 -a 'tar -xf /opt/nginx-1.22.0.tar.gz -C /opt' //常用的参数: chdir:在远程主机上运行命令前提前进入目录 creates:判断指定文件是否存在,如果存在,不执行后面的操作 removes:判断指定文件是否存在,如果存在,执行后面的操作 ansible all -m command -a "chdir=/home ls ./" ansible all -m command -a "creates=/opt/123 ls /opt" ansible all -m command -a "removes=/opt/test.txt ls /opt" 2.shell 模块 //在远程主机执行命令,相当于调用远程主机的shell进程, 然后在该shell下打开一个子shell运行命令(支持管道符号等功能) ansible-doc -s shell ansible webservers -m shell -a 'useradd test' ansible webservers -m shell -a 'echo 123456 \| passwd --stdin test' ansible webservers -m shell -a 'echo $(ifconfig ens33 \| awk "NR==2 {print $2}") \| cut -d " " -f2' ansible webservers -m shell -a 'echo $(ifconfig ens33 \| awk "NR==2 {print $2}")' ansible 192.168.233.20 -m shell -a 'touch /opt/123 \&\& echo 123 \> /opt/123 \&\& cat /opt/123' #多个shell命令可以用\&\&联合在一块,表示逻辑且 ansible 192.168.233.30 -m shell -a 'touch /opt/123 ; echo 123 \> /opt/123 ; cat /opt/123' #也可以用;号联合,表示逻辑或 #远程创建一个脚本 \[root@test1 opt\]# ansible 192.168.233.30 -m shell -a 'echo -e "#!/bin/bash\\nifconfig" \> /opt/test1.sh' 192.168.233.30 \| CHANGED \| rc=0 \>\> #远程运行脚本 \[root@test1 opt\]# ansible 192.168.233.30 -m shell -a 'echo -e "#!/bin/bash\\nifconfig" \> /opt/test1.sh \&\& sh /opt/test1.sh' 3.cron 模块 //在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。 ansible-doc -s cron #按 q 退出 //常用的参数: minute/hour/day/month/weekday:分/时/日/月/周 job:任务计划要执行的命令 name:任务计划的名称 ansible webservers -m cron -a 'minute="\*/1" job="/bin/echo helloworld" name="test1"' ansible webservers -m cron -a 'minute=30 hour=8 day=1 month=9 job="ls /opt" name="test2"' ansible webservers -m cron -a 'minute=30 hour=8 weekday=3 job="ls /etc"' ansible webservers -a 'crontab -l' ansible webservers -m cron -a 'minute=30 hour=8 weekday=3 job="ls /etc"' #创建计划任务时可以不起名字 ansible webservers -m cron -a 'name="test1" state=absent' #移除计划任务,假如该计划任务没有取名字,name=None即可 4.user 模块 //用户管理的模块 ansible-doc -s user //常用的参数: name:用户名,必选参数 state=present\|absent:创建账号或者删除账号,present表示创建,absent表示删除 system=yes\|no:是否为系统账号 uid:用户uid group:用户基本组 shell:默认使用的shell create_home=yes\|no:替换用户的家目录 password:用户的密码,建议使用加密后的字符串 comment:用户的注释信息 remove=yes\|no:当state=absent时,是否删除用户的家目录 ansible dbservers -m user -a 'name=test system=no' #创建用户test01 #将test01用户添加到mysql组中,system=yes表示这是一个系统用户, system=yes 仅仅标志这是一个系统用户, 但没有明确指定 shell 的情况下,会使用系统默认的 shell ansible 192.168.233.30 -m user -a 'name=test1 uid=1001 shell=/sbin/nologin password=123456' ansible 192.168.233.30 -m user -a 'name=test1 home=/opt/test1 create_home=yes' #更改用户的家目录 ansible dbservers -m command -a 'tail /etc/passwd' ansible 192.168.233.30 -m user -a 'name=test1 remove=yes state=absent' #删除用户和用户的家目录 ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01 5.group 模块 //用户组管理的模块 ansible-doc -s group ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #创建mysql组 ansible dbservers -a 'tail /etc/group' #注意一点,Ansible 会连接到远程主机、执行该命令,然后立即退出。 因此,对于 tail -f 这样的交互式命令不会有任何结果 ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' ansible dbservers -m user -a 'name=test02 uid=306 system=yes group=mysql shell=/sbin/nologin' ansible dbservers -a 'tail /etc/passwd' ansible dbservers -a 'id test01' 6.copy 模块 //用于复制指定主机文件到远程主机的 ansible-doc -s copy //常用的参数: dest:指出复制文件的目标及位置,使用绝对路径,如果源是目录,指目标也要是目录, 如果目标文件已经存在会覆盖原有的内容 src:指出源文件的路径,可以使用相对路径或绝对路径, 支持直接指定目录,如果源是目录则目标也要是目录 mode:指出复制时,目标文件的权限 owner:指出复制时,目标文件的属主 group:指出复制时,目标文件的属组 content:指出复制到目标主机上的内容,不能与src一起使用 ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640' ansible dbservers -a 'ls -l /opt' ansible dbservers -a 'cat /opt/fstab.bak' ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #将helloworld写入/opt/hello.txt文件中 ansible dbservers -a 'cat /opt/hello.txt' #没有专门的mv模块 ansible dbservers -a 'mv /opt/123 /opt/123.txt' 7.file 模块 //设置文件属性 ansible-doc -s file ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak' #修改文件的属主属组权限等 ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link' #设置/opt/fstab.link为/opt/fstab.bak的链接文件 ansible dbservers -m file -a "path=/opt/abc.txt state=touch" #创建一个文件 ansible dbservers -m file -a "path=/opt/abc.txt state=absent" #删除一个文件 8.hostname 模块 //用于管理远程主机上的主机名 ansible dbservers -m hostname -a "name=mysql01" 9.ping 模块 //检测远程主机的连通性 ansible all -m ping 10.yum 模块 //在远程主机上安装与卸载软件包 ansible-doc -s yum ansible webservers -m yum -a 'name=httpd' #安装服务 ansible webservers -m yum -a 'name=httpd state=absent' #卸载服务 11.service/systemd 模块 //用于管理远程主机上的管理服务的运行状态 ansible-doc -s service //常用的参数: name:被管理的服务名称 state=started\|stopped\|restarted:动作包含启动关闭或者重启 enabled=true:表示是否设置该服务开机自启 runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动 ansible webservers -a 'systemctl status httpd' #查看web服务器httpd运行状态 ansible 192.168.233.20 -m shell -a 'echo "this is test" \> /var/www/html/index.html' #改变页面内容 ansible webservers -m service -a 'enabled=true name=httpd state=started' ansible webservers -m service -a 'enabled=true name=httpd state=started runlevel=40' #启动httpd服务 ansible webservers -m yum -a 'name=nginx' ansible webservers -a 'systemctl status nginx' ansible webservers -m service -a 'enabled=true name=nginx state=started' ansible 192.168.233.20 -m shell -a 'curl 192.168.233.20' 12、防火墙和网络模块: 1、ipatbles 禁止ping ansible 192.168.233.10 -m iptables -a "chain=INPUT protocol=ICMP source=192.168.233.20 jump=REJECT" -b #禁用80端口 ansible 192.168.233.13 -m iptables -a "chain=INPUT protocol=tcp destination_port=80 jump=REJECT" -b #删除策略 ansible 192.168.233.13 -m iptables -a "chain=INPUT protocol=tcp destination_port=80 jump=REJECT state=absent" -b 2、firewalld ansible 192.168.233.13 -m firewalld -a "service=nginx zone=public permanent=true state=enabled immediate=true" -b #报错,因为firewalld支持的服务当中没有nginx firewall-cmd --get-services \| grep nginx #没有支持nginx的类型 ansible 192.168.233.13 -m firewalld -a "port=80/tcp zone=public permanent=true state=enabled immediate=true" -b #换成端口 #删除策略 ansible 192.168.233.13 -m firewalld -a "port=80/tcp zone=public permanent=true state=disabled immediate=true" -b 3、配置网卡: ansible 192.168.233.13 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='\^IPADDR=' line='IPADDR=192.168.233.19'" -b ansible 192.168.233.19 -m ansible.builtin.systemd -a "name=network state=restarted" -b 13.script 模块 //运行本地脚本,执行的是目标主机的命令 然后把结果输出到目标主机 ansible-doc -s script vim test.sh #!/bin/bash echo "hello ansible from script" \> /opt/script.txt chmod +x test.sh ansible webservers -m script -a 'test.sh' ansible webservers -a 'cat /opt/script.txt' 14.setup 模块 //facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息 ansible-doc -s setup ansible webservers -m setup #获取mysql组主机的facts信息 ansible dbservers -m setup -a 'filter=\*ipv4' #使用filter可以筛选指定的facts信息 ansible webservers -m setup -a 'filter=ansible_*processor*' #查看cpu ansible webservers -m setup -a 'filter=ansible_mem\*' #查看内存 ansible webservers -m setup -a 'filter=ansible_os_family' #查看系统 ansible webservers -m setup -a 'filter=ansible_proc_cmdline' #查看内核 ansible webservers -m setup -a 'filter=ansible_system' #查看系统信息 --------- inventory 主机清单 --------- //Inventory支持对主机进行分组,每个组内可以定义多个主机, 每个主机都可以定义在任何一个或多个主机组内。 71 host_key_checking = False #取消注释,取消密钥队验证 //如果是名称类似的主机,可以使用列表的方式标识各个主机。 vim /etc/ansible/hosts \[webservers\] 192.168.80.11:2222 #冒号后定义远程连接端口,默认是 ssh 的 22 端口 192.168.233.6\[1:3\] #表示ip地址范围,1-3 \[dbservers\] db-\[a:f\].example.org #支持匹配 a\~f //inventory 中的变量 Inventory变量名 含义 ansible_host ansible连接节点时的IP地址 ansible_port 连接对方的端口号,ssh连接时默认为22 ansible_user 连接对方主机时使用的用户名。不指定时, 将使用执行ansible或ansible-playbook命令的用户 ansible_password 连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效 ansible_ssh_private_key_file 指定密钥认证ssh连接时的私钥文件 ansible_ssh_common_args 提供给ssh、sftp、scp命令的额外参数 ansible_become 允许进行权限提升 ansible_become_method 指定提升权限的方式,例如可使用sudo/su/runas等方式 ansible_become_user 提升为哪个用户的权限,默认提升为root ansible_become_password 提升为指定用户权限时的密码 (1)主机变量 \[webservers\] 192.168.233.40 ansible_port=22 ansible_user=root ansible_password=123 (2)组变量 \[webservers:vars\] #表示为 webservers 组内所有主机定义变量 ansible_user=root ansible_password=123 \[all:vars\] #表示为所有组内的所有主机定义变量 ansible_port=22 (3)组嵌套 \[nginx\] 192.168.80.20 192.168.80.21 192.168.80.22 \[apache\] 192.168.80.3\[0:3

webs:children\] #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机 nginx apache

相关推荐
会又不会4 分钟前
Jenkins-Email Extension 插件插件
运维·jenkins
电脑能手1 小时前
[保姆级教程] 解决不同局域网电脑无法SSH的问题
运维·ssh·电脑
czhc11400756631 小时前
Linux 76 rsync
linux·运维·python
你不知道我是谁?2 小时前
负载均衡--四层、七层负载均衡的区别
运维·服务器·负载均衡
dyj0953 小时前
【Rancher Server + Kubernets】- Nginx-ingress日志持久化至宿主机
运维·nginx·rancher
码出钞能力4 小时前
linux内核模块的查看
linux·运维·服务器
星辰云-4 小时前
# Linux Centos系统硬盘分区扩容
linux·运维·centos·磁盘扩容
Hellc0075 小时前
Nginx 高级 CC 与 DDoS 防御策略指南
运维·nginx·ddos
feilieren5 小时前
Docker 安装 Elasticsearch 9
运维·elasticsearch·docker·es
小皮侠6 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github