第三十六天:Ansible安装和常见问题
一、自动化运维应用场景
1、运维职业发展路线
2、企业实际应用场景分析
DEV开始环境-》测试环境-》预发布环境-》发布环境-》生产环境-》灰度环境
3、常见自动化运维工具
2、Ansible 功能
3、Ansible 特点
(1)优点
4、Ansible 架构
(1)Ansible 组成
1、Ansible 安装
1 2 | https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html https://docs.ansible.com/ansible/latest/installation_guide/ index .html |
1 | https://releases.ansible.com/ansible/ |
1 | https://pypi.org/project/ansible/ |
(1)包安装方式
1 2 3 4 | #CentOS 的EPEL源的rpm包安装 [root@centos ~]#yum install ansible #ubuntu 安装 [root@ubuntu ~]#apt -y install ansible |
(2)pip 安装
1 2 3 4 5 6 7 8 | [root@rocky8 ~]#yum -y install python39 rust [root@rocky8 ~]#pip3 install ansible [root@rocky8 ~]#ansible --version [root@rocky8 ~]#yum -y install python38 python38-pip [root@rocky8 ~]#pip3 install --upgrade pip -i https://pypi.douban.com/simple [root@rocky8 ~]#pip3 install ansible -i https://pypi.douban.com/simple/ [root@rocky8 ~]#ansible --version |
2、Ansible 相关文件
(1)Ansible 配置文件列表
(2)Ansible 主配置文件
1 2 3 4 5 | ANSIBLE_CONFIG #环境变量,注意:指定目录下的ansible.cfg文件必须存在才能生效 ./ansible.cfg #当前目录下的ansible.cfg,一般一个项目对应一个专用配置文件,推荐使 用 ~/.ansible.cfg #当前用户家目录下的.ansible.cfg /etc/ansible/ansible.cfg #系统默认配置文件 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [defaults] #inventory = /etc/ansible/hosts #主机列表配置文件 #library = /usr/share/my_modules/ #库文件存放目录 #remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录 #local_tmp = $HOME/.ansible/tmp #本机的临时命令执行目录 #forks = 5 #默认并发数 #sudo_user = root #默认sudo 用户 #ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码 #ask_pass = True #remote_port = 22 #host_key_checking = False #检查对应服务器的host_key,建议取消此行注释,实现第一次连 接自动信任目标主机 #log_path=/var/log/ansible.log #日志文件,建议启用 #module_name = command #默认模块,可以修改为shell模块 [privilege_escalation] #普通用户提权配置 #become= True #become_method=sudo #become_user=root #become_ask_pass= False |
(3) Inventory 主机清单文件
1 | https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html |
3、Ansible相关工具
/usr/bin/ansible 主程序,临时命令执行工具
(1)ansible-doc
此工具用来显示模块帮助,相当于man
1 2 3 | ansible-doc [options] [module...] -l, --list #列出可用模块 -s, --snippet #显示指定模块的playbook片段 |
(2)ansible
1 | ansible <host-pattern> [-m module_name] [-a args] |
(3)ansible-console
此工具可交互执行命令,支持tab,ansible 2.0+新增
1 | 执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$ |
常用子命令:
4、Ansible常用模块
1 2 3 4 | https://docs.ansible.com/ansible/2.9/modules/modules_by_category.html https://docs.ansible.com/ansible/2.9/modules/list_of_all_modules.html https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html https://docs.ansible.com/ansible/latest/modules/modules_by_category.html |
(1)Command 模块
1 2 3 | chdir=dir #执行命令前,先切换至目录dir creates=file #当file不存在时才会执行 removes=file #当file存在时才会执行 |
(2)Shell 模块
1 2 3 | chdir=dir #执行命令前,先切换至目录dir creates=file #当file不存在时才会执行 removes=file #当file存在时才会执行 |
(3)Script 模块
1 2 3 4 | chdir=dir #执行命令前,先切换至目录dir cmd #指定ansible主机的命令 creates=file #当file不存在时才会执行 removes=file #当file存在时才会执行 |
(4)Copy 模块
1 2 3 4 5 6 7 8 | src #控制端的源文件路径 dest #被控端的文件路径 owner #属主 group #属组 mode #权限 backup #是否备份 validate #验证成功才会执行copy remote_src # no 是默认值,表示src文件在ansible主机,yes表示src文件在远程主机 |
(6)Fetch 模块
1 2 | src #被控制端的源文件路径,只支持文件 dest #ansible控制端的目录路径 |
(7)File 模块
(8)stat 模块
1 | path #文件/对象的完整路径(必须) |
1 2 | exists: 判断是否存在 isuid: 调用用户的ID与所有者ID是否匹配 |
(9)unarchive 模块
(10)Archive 模块
1 2 3 | path #压缩的文件或目录 dest #压缩后的文件 format #压缩格式,支持gz,bz2,xz,tar,zip |
(11)Hostname 模块
1 | name #修改后的主机名称 |
(12)Cron 模块
1 2 3 4 5 6 | name #描述脚本的作用 minute #分钟 hour #小时 weekday #周 user #任务由哪个用户运行;默认root job #任务 |
(13)Yum 和 Apt 模块
1 2 3 4 5 6 7 8 9 10 | name #软件包名称 state #状态 =present #安装,此为默认值 =absent #删除 =latest #最新版 list #列出指定包 enablerepo #启用哪个仓库安装 disablerepo #不使用哪些仓库的包 exclude #排除指定的包 validate #是否检验,默认为yes |
(14)yum_repository 模块
1 2 3 4 5 | name #仓库id description #仓库描述名称,对应配置文件中的 name = baseurl #仓库的地址 gpgcheck #验证开启 gpgkey #仓库公钥路径 |
(15)Service 模块
1 2 3 4 5 6 7 8 | name #服务名称 state #服务状态 =started #启动 =stopped #停止 =restarted #重启 =reloaded #重载 enabled #开启自启动 daemon_reload #加载新的配置文件,适用于systemd模块 |
(16)User 模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | name #创建的名称 uid #指定uid group #指定基本组 shell #登录shell类型默认/bin/bash create_home #是否创建家目录 password #设定对应的密码,必须是加密后的字符串才行,否则不生效 system #yes表示系统用户 groups #附加组 append #追加附加组使用,yes表示增加新的附加组 state #absen删除 remove #yes表示删除用户时将家目录一起删除 generate_ssh_key #创建私钥 ssh_keyu_bits #私钥位数 ssh_key_file #私钥文件路径 |
(17)Group 模块
1 2 3 4 5 | name #指定组名称 gid #指定gid state =present #创建,默认 =absent #删除 |
(18)Lineinfile 模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | path #被控端文件的路径 regexp #正则匹配语法格式,表示被替换的内容 line #替换为的内容 state #absent表示删除 insertafter #插入到替换内容前面,如和regexp同时存在,只在没找到与regexp匹配时才使用 insertafter insertbefore #插入到替换内容后面,如和regexp同时存在,只在没找到与regexp匹配时才使用 insertafter backrefs #支持后面引用,yes和 no backup #修改前先备份 create #如果文件不存在,则创建,默认不存在会出错 mode #指定权限 owner #指定用户 group #指定组 #注意 regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被 匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。 |
(19)Replace 模块
1 2 3 4 5 6 7 8 9 | path #被控端文件的路径 regexp #正则匹配语法格式,表示被替换的内容 replace #替换为的内容 after #插入到替换内容前面, before #插入到替换内容后面 backup #修改前先备份 mode #指定权限 owner #指定用户 group #指定组 |
(20)SELinux 模块
1 2 | policy #指定SELINUXTYPE=targeted state #指定SELINUX=disabled |
(21)reboot 模块
1 2 3 4 5 | msg #重启提示 pre_reboot_delay #重启前延迟时间的秒数 post_reboot_delay #重启后延迟时间的秒数后,再验证系统正常启动 reboot_timeout #重启后延迟时间再执行测试成功与否的命令 test_command #执行测试成功与否的命令 |
(22)mount 模块
1 2 3 4 5 6 7 8 9 | src #源设备路径,或网络地址 path #挂载至本地哪个路径下 fstype #设备类型; nfs opts #挂载的选项 state #挂载还是卸载 =present #永久挂载,但没有立即生效 =absent #卸载临时挂载,并删除永久挂载 =mounted #临时挂载 =unmounted #临时卸载 |
(23)Setup 模块
1 | filter #指定过滤条件 |
(24)debug 模块
1 2 3 | msg #指定命令输出的信息 var #指定变量名,和msg互斥 verbosity #详细度 |
(25)sysctl 模块
1 2 3 4 | name #内核参数 value #指定值 state #是否保存在sysctl.conf文件中,默认present sysctl_set #使用sysctl -w 验证值生效 |
(26)pam_limits
1 2 3 4 5 6 7 8 9 | - name : Change Limit /etc/security/limit.conf pam_limits: domain: "*" limit_type: "{{ item.limit_type }}" limit_item: "{{ item.limit_item }}" value: "{{ item.value }}" loop: - { limit_type: 'soft' , limit_item: 'nofile' ,value: '100000' } - { limit_type: 'hard' , limit_item: 'nofile' ,value: '10000' } |
(27)apt_repository 模块
1 2 3 4 | repo #仓库信息 state #添加或删除 update_cache #是否apt update ,默认yes filename #仓库文件,默认放在/etc/apt/sources.list.d/file.list |
(28) apt_key 模块
1 2 | url # key 路径 state #添加或删除 |
(29)其它模块
1 2 3 4 5 6 7 8 9 | nginx_status_info nginx_status_facts mysql_db #需要安装MySQL-python包 mysql_user #需要安装MySQL-python包 redis mongodb* postgresql* haproxy git |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库