首页 > 其他分享 >ansible自动化运维工具

ansible自动化运维工具

时间:2023-02-06 16:23:35浏览次数:45  
标签:Ansible 运维 webservers 主机 自动化 ansible 模块 dbservers

一、Ansible

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

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

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

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

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

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

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

二、Ansible环境安装部署

1、环境准备

1 2 3 管理端:192.168.142.3   ansible 被管理端:192.168.142.4 被管理端:192.168.142.5

2、管理端安装 ansible

1 2 3 4 5 6 7 8 yum install -y epel-release         //先安装 epel 源 yum install -y ansible   #ansible目录结构 ls /etc/ansible  ansible.cfg        #ansible的配置文件,一般无需修改  hosts              #ansible的主机清单,用于存储需要管理的远程主机的相关信息  roles/             #公共角色目录

 

3、配置主机清单

1 2 3 4 5 6 7 cd /etc/ansible vim hosts       [webservers]            #配置组名 192.168.142.4           #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)   [dbservers] 192.168.142.5

 

4、配置密钥对验证

1 2 3 4 #管理端 ssh-keygen -t rsa       #一路回车,使用免密登录 ssh-copy-id [email protected]   #设置密钥对进行免密登录,首次登录要输入密码 ssh-copy-id [email protected]

 

 

三、ansible命令行模块

1 2 命令格式:ansible <组名> -m <模块> -a <参数列表> ansible-doc -l              #列出所有已安装的模块,按q退出

 

1、command模块

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 在远程主机执行命令,不支持管道,重定向等shell的特性。 ansible-doc -s command      #-s 列出指定模块的描述信息和操作动作   ansible 192.168.142.4 -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 模块   ========================================================== 常用的参数: chdir:在远程主机上运行命令前提前进入目录 creates:判断指定文件是否存在,如果存在,不执行后面的操作 removes:判断指定文件是否存在,如果存在,执行后面的操作 ========================================================== ansible all -m command -a "chdir=/home ls ./"

 

2、shell模块

1 2 3 4 5 6 在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能) ansible-doc -s shell   ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'    #给dbservers组的主机中test用户设置密码 ansible webservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'   #截取网卡中的主机ip ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'                   #截取网卡中的主机ip,这边转义符\用来让$2代表通用变量名

  

3、cron 模块

1 2 3 4 5 6 7 8 9 10 11 12 13 在远程主机定义任务计划。其中有两种状态(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 woshidashuaige" name="test crontab"'   #每隔1分钟执行名称是test crontab任务 ansible webservers -a 'crontab -l'  #查看任务列表 ansible webservers -m cron -a 'name="test crontab" state=absent'  #移除计划任务,假如该计划任务没有取名字,name=None即可

 

4、user模块

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 用户管理的模块 ansible-doc -s user ========================================================== 常用的参数: name:用户名,必选参数 state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除 system=yes|no:是否为系统账号 uid:用户uid group:用户基本组 shell:默认使用的shell move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动 password:用户的密码,建议使用加密后的字符串 comment:用户的注释信息 remove=yes|no:当state=absent时,是否删除用户的家目录 ========================================================== ansible dbservers -m user -a 'name="test1"'             #创建用户test01 ansible dbservers -a 'tail /etc/passwd' ansible dbservers -m user -a 'name="test1" state=absent remove=yes' #删除用户test01并且删除家目录

 

5、group模块

1 2 3 4 5 6 7 8 用户组管理的模块 ansible-doc -s group   ansible dbservers -m group -a 'name=mysql gid=250 system=yes'   #创建mysql组 ansible dbservers -a 'tail /etc/group' ansible dbservers -m user -a 'name=test1 uid=250 system=yes group=mysql' #将test1用户添加到mysql组中 ansible dbservers -a 'tail /etc/passwd' ansible dbservers -a 'id test1'  

 

6、copy模块

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 用于复制指定主机文件到远程主机的 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 group=mysql mode=777'   #复制本机/etc/fstab到目标主机,文件属主root,属组mysql,权限777 ansible dbservers -a 'ls -l /opt/fstab.bak'   ansible dbservers -a 'cat /opt/fstab.bak' ansible dbservers -m copy -a 'content="aaaaa" dest=/opt/fstab.bak'  #将aaaaa写入/opt/fstab.bak文件中(覆盖数据) ansible dbservers -a 'cat /opt/fstab.bak'

 

7、file模块

1 2 3 4 5 6 设置文件属性 ansible-doc -s file ========================================================== ansible webservers -m file -a 'state=touch owner=test2 group=mysql mode=644 path=/opt/fstab.bak'    #创建文件并且修改文件的属主属组权限等、已存在的文件不用加touch指令 ansible webservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'    #设置/opt/fstab.link为/opt/fstab.bak的链接文件 ansible webservers -m file -a "path=/opt/abc.txt state=absent"          #删除一个文件

   

8、hostname模块

1 2 用于管理远程主机上的主机名 ansible dbservers -m hostname -a "name=text3"

 

9、ping模块

1 2 检测远程主机的连通性 ansible all -m ping

10、yum模块

1 2 3 4 5 在远程主机上安装与卸载软件包 ansible-doc -s yum   ansible webservers -m yum -a 'name=httpd'           #安装服务 ansible webservers -m yum -a 'name=httpd state=absent'      #卸载服务

 

 

11、service/systemd模块

1 2 3 4 5 6 7 8 9 10 11 用于管理远程主机上的管理服务的运行状态 ansible-doc -s service ========================================================== 常用的参数: name:被管理的服务名称 state=started|stopped|restarted:动作包含启动关闭或者重启 enabled=yes|no:表示是否设置该服务开机自启 runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动 ========================================================== ansible webservers -a 'systemctl status httpd'          #查看web服务器httpd运行状态 ansible webservers -m service -a 'enabled=true name=httpd state=started'            #启动httpd服务

12、script模块

1 2 3 4 5 6 7 8 9 实现远程批量运行本地的 shell 脚本 ansible-doc -s script ========================================================== vim test.sh #!/bin/bash echo "bbbbb" > /opt/123.txt ========================================================== ansible webservers -m script -a 'test.sh' ansible webservers -a 'cat /opt/123.txt'

 

 

13、setup模块

1 2 3 4 5 facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息 ansible-doc -s setup ========================================================== ansible webservers -m setup             #获取mysql组主机的facts信息 ansible dbservers -m setup -a 'filter=*ipv4'    #使用filter可以筛选指定的facts信息

 

四、inventory主机清单

1、Inventory主机清单支持的标识

1 2 3 4 5 6 7 8 9 10 Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内   如果是名称类似的主机,可以使用列表的方式标识各个主机 vim /etc/ansible/hosts [webservers] 192.168.142.4:2222      #冒号后定义远程连接端口,默认是 ssh 的 22 端口 192.168.142.[5:6]   [dbservers] db-[a:f].example.org    #支持匹配 a~f

2、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)主机变量

1 2 [dbservers] 192.168.142.5 ansible_port=22 ansible_user=root ansible_password=123456

 

(2)组变量

1 2 3 4 5 6 [dbservers:vars]            #表示为 webservers 组内所有主机定义变量 ansible_user=root ansible_password=123456   [all:vars]                  #表示为所有组内的所有主机定义变量 ansible_port=22

 

(3)组嵌套

1 2 3 4 5 6 7 8 9 10 11 [webservers] 192.168.142.4   [dbservers] 192.168.142.5   [webs:children]     #表示为webs主机组中包含了nginx组和apache 组内的所有主机 webservers dbservers   ansible webs(父组) -m ping来测试

 

标签:Ansible,运维,webservers,主机,自动化,ansible,模块,dbservers
From: https://www.cnblogs.com/y0226/p/17069657.html

相关文章