尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

Linux运维7.2——ansible

Linux运维7.2——ansible 文章目录Linux运维7.2ansible概念Ansible 是什么核心架构组成1. 控制节点Control Node2. 被管理节点Managed Node / 被控主机3. Inventory 主机清单4. Module 模块5. Ad-hoc 临时命令6. Playbook 剧本7. Role 角色8. Facts 资产信息9. Handler 处理器10. Template 模板核心通信原理三种返回状态简单区分概念安装配置yum 安装pip 安装Ansible 命令主机连通command模块file模块copy模块Ansible Playbook示例——httpd示例——返回httpd网页内容示例——keepalived示例——批量创建用户Linux运维7.2ansible概念Ansible 是什么Ansible是一款开源、无代理agentless的自动化运维工具使用 Python 开发可以实现批量远程执行命令、配置管理、应用部署、持续交付、编排任务。核心特点不需要在被管理机器安装客户端依托 SSH 通信。核心架构组成1. 控制节点Control Node运行 ansible 的服务器只需要一台所有任务从控制节点发起依赖Python、OpenSSH。2. 被管理节点Managed Node / 被控主机被批量操作的服务器只需满足开启 SSH、自带 Python绝大多数 Linux 默认具备。3. Inventory 主机清单定义所有被控主机列表Ansible 知道要操作哪些机器。默认文件/etc/ansible/hosts支持分组、变量、主机别名、动态清单。示例[web]192.168.1.10192.168.1.11[db]192.168.1.204. Module 模块Ansible 的最小执行单元相当于一条条封装好的功能函数。执行单条命令ansible 主机-m模块名常见模块command/shell/copy/file/yum/apt/service/usercommand不支持管道shell支持管道、特殊符号5. Ad-hoc 临时命令一次性执行的简短命令适合简单批量操作不保存任务。示例ansible web -m shell -a df -h6. Playbook 剧本使用YAML语言编写的任务文件批量编排多步骤任务可以持久保存、重复执行。Play定义对哪一组主机执行任务Task一个个要执行的动作调用模块简易结构-name:部署nginxhosts:webtasks:-name:安装nginxyum:namenginx statepresent-name:启动服务service:namenginx statestarted enabledyes7. Role 角色Playbook 的规范化封装用于复用、模块化大型项目。固定目录结构把变量、任务、模板、文件分离多套环境直接调用 role企业主流用法。8. Facts 资产信息Ansible 连接主机后自动采集的主机信息CPU、内存、IP、系统版本等可以直接在 playbook 中作为变量引用。9. Handler 处理器只有当changed 状态触发才执行的任务多用于服务重启。例如配置文件修改后才执行重启 nginx。10. Template 模板基于 Jinja2 模板语法的配置文件可以写入变量推送至远端主机。后缀一般.j2。核心通信原理控制节点 → SSH → 被控节点控制节点生成临时 Python 脚本通过 SSH 传输到远端远端执行脚本并返回结果执行完毕自动删除临时脚本三种返回状态ok成功没有发生变更changed成功主机资源发生修改安装软件、修改文件failed执行失败简单区分概念Ad-hoc临时单行命令一次性Playbookyaml 脚本多任务流程Role标准化打包的 playbook方便复用Inventory管理主机名单Module实际干活的功能单元安装配置yum 安装配置EPEL网络yum源[rootansible ~]# yum install -y epel-release安装ansible[rootansible ~]# yum install ansible -ypip 安装[rootansible ~]# yum install python-pip[rootansible ~]# pip install ansible激活配置文件[rootserver1 ~]# cat /etc/ansible/ansible.cfg[rootserver1 ~]# ansible-config init --disabled ansible.cfg配置文件生成[rootserver1 ansible]# vim /etc/ansible/ansible.cfgInventory 主机清单配置文件[rootserver1 ansible]# vim /etc/ansible/hosts查看主机配置信息[rootserver1 ansible]# ansible-inventory --graphAnsible 命令主机连通[rootserver ~]# ansible web -m ping注意需要免密配置或者主机之间至少远程连接过一次command模块示例——获取主机名file模块增加文件ansible rocky9-atouch /tmp/test.txt删除文件ansible rocky9 -a rm -f /tmp/test.txt创建目录[rootserver ~]# ansible web -m file -a path/data/app statedirectory创建链接文件path链接文件src源文件[rootserver ~]# ansible web -m file -a path/data/bbb.jpg srcaaa.jpg statelinkcopy模块src#被复制到远程主机的本地文件。可以是绝对路径也可以是相对路径。如果路径是一个目录则会递 归复制用法类似于rsynccontent#用于替换src可以直接指定文件的内容dest#必选项将源文件复制到的远程主机的**绝对路径**backup#当文件内容发生改变后在覆盖之前把源文件备份备份文件包含时间信息directory_mode#递归设定目录的权限默认为系统默认权限force#当目标主机包含该文件但内容不同时设为yes表示强制覆盖设为no表示目标主机的 目标位置不存在该文件才复制。默认为yesothers#所有的 file 模块中的选项可以在这里使用复制文件[rootserver ~]# ansible web -m copy -a src~/hello dest/data/hello给定内容生成文件并制定权限[rootserver ~]# ansible web -m copy -a contentI am keer\n dest/data/name mode666覆盖[rootserver ~]# ansible web -m copy -a contentI am keerya\n backupyes dest/data/name mode666fetch模块该模块用于从远程某主机获取复制文件到本地。dest#用来存放文件的目录src#在远程拉取的文件并且必须是一个file不能是目录cron模块用于管理cron计划任务的。day# 日应该运行的工作( 1-31, *, */2, )hour# 小时 ( 0-23, *, */2, )minute# 分钟( 0-59, *, */2, )month# 月( 1-12, * , */2, )weekday# 周 ( 0-6 for Sunday-Saturday,, )job# 指明运行的命令是什么name# 定时任务描述reboot# 任务在重启时运行不建议使用建议使用special_timespecial_time# 特殊的时间范围参数reboot重启时annually每年monthly每月weekly 每周daily每天hourly每小时 state# 指定状态present表示添加定时任务也是默认设置absent表示删除定时任务user# 以哪个用户的身份执行yum模块name #所安装的包的名称 state #present---安装 latest---安装最新的, absent--- 卸载软件。 update_cache #强制更新yum的缓存 conf_file #指定远程yum安装时所依赖的配置文件安装本地已有的包。 disable_pgp_check #是否禁止GPG checking只用于presentor latest。 disablerepo #临时禁止使用yum库。 只用于安装或更新时。 enablerepo #临时使用的yum库。只用于安装或更新时。service模块arguments#命令行提供额外的参数enabled#设置开机启动。 yes/true 开机自启 no/flase 开机关闭name#服务名称runlevel#开机启动的级别一般不用指定。sleep#在重启服务的过程中是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)state#有四种状态分别为#started---启动服务#stopped---停止服务#restarted---重启服务#reloaded---重载配置user模块comment# 用户的描述信息createhome# 是否创建家目录force# 在使用stateabsent时, 行为与userdel –force一致.group# 指定基本组groups# 指定附加组如果指定为(groups)表示删除所有组home# 指定用户家目录move_home# 如果设置为home时, 试图将用户主目录移动到指定的目录name# 指定用户名non_unique# 该选项允许改变非唯一的用户ID值password# 指定用户密码remove# 在使用stateabsent时, 行为是与userdel –remove一致; removeyes //删除干净shell# 指定默认shellsystem# 当创建一个用户设置这个用户是系统用户。这个设置不能更改现有用户uid# 指定用户的uidgroup模块gid#设置组的GID号name#指定组的名称state#指定组的状态默认为创建设置值为absent为删除system#设置值为yes表示创建为系统组script模块该模块用于将本机的脚本在被管理端的机器上运行。[rootserver ~]# vim /tmp/df.sh#!/bin/bashdate/tmp/disk_total.logdf-lh/tmp/disk_total.log[rootserver ~]# chmod x /tmp/df.shsetup模块该模块主要用于收集信息是通过调用facts组件来实现的。facts组件是Ansible用于采集被管机器设备信息的一个功能我们可以使用setup模块查机器的所有facts信息可以使用filter来查看指定信 息。整个facts信息被包装在一个JSON格式的数据结构中ansible_facts是最上层的值。facts就是变量内建变量 每个主机的各种信息cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信 息在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装而debian系列用apt来安装软件。lineinfile功能修改或删除文件内容与系统中的 sed 命令类似主要参数如下 path#指定要操作的文件regexp#使用正则表达式匹配对应的行line#修改为新的内容insertafter#将文本插入到“指定的行”之后insertbefore#将文本插入到“指定的行”之前state#删除对应的文本时需要stateabsentbackrefs#1.支持后向引用、2.当未匹配到内容则不操作文件backup#是否在修改文件之前对文件进行备份create#当要操作的文件并不存在时是否创建对应的文件Ansible Playbook示例——httpd--- - hosts: web become:yestasks: - name: Install the Apache ansible.builtin.yum: name: httpd state: present - name: Startservicehttpd,ifnot started ansible.builtin.service: name: httpd state: started enable:yes- name: creat index.html ansible.builtin.copy: content:www.westos.org\ndest: /var/www/html/index.html测试运行[devopsserver1 ansible]$ ansible-playbook apache.yml[devopsserver1 ~]$curlserver2示例——返回httpd网页内容--- - hosts: lamp become: yes tasks: - name: Install the Apache ansible.builtin.yum: name: httpd state: present - name: Start service httpd, if not started ansible.builtin.service: name: httpd state: started enabled: yes - name: creat index.html ansible.builtin.copy: content: {{ ansible_hostname }}\n dest: /var/www/html/index.html - name: Ensure the default Apaceh port is 80 ansible.builtin.lineinfile: path: /etc/httpd/conf/httpd.conf regexp: ^Listen insertafter: ^#Listen line: Listen 80 notify: restart service httpd handlers: - name: restart service httpd ansible.builtin.service: name: httpd state: restarted - hosts: localhost gather_facts: false become: false tasks: - name: Check that you can connect (GET) to a page and it returns a status 200 ansible.builtin.uri: url: http://192.168.234.162 return_content: true register: result - name: Print return information from the previous task ansible.builtin.debug: var: result示例——keepalived[devopsserver1 root]$cd/home/devops/ansible/[devopsserver1 ansible]$sudovim/etc/ansible/hosts[hacluster]192.168.234.161 stateMASTER pri100192.168.234.162 stateBACKUP pri80[hacluster:vars]interfaceeth0router_id61vip192.168.234.200[devopsserver1 ansible]$vimkeepalived.yml --- - hosts: hacluster#become: yestasks: - name: Install the keepalived ansible.builtin.yum: name: keepalived state: present - name: configure the keepalived ansible.builtin.template: src: keepalived.conf.j2 dest: /etc/keepalived/keepalived.conf notify: restartservicekeepalived - name: Startservicekepalived ansible.builtin.service: name: keepalived state: started enabled:yeshandlers: - name: restartservicekeepalived ansible.builtin.service: name: keepalived state: restarted[devopsserver1 ansible]$ yuminstall-ykeepalived.x86_64[devopsserver1 ansible]$cd[devopsserver1 ~]$cd/etc/keepalived/[devopsserver1 keepalived]$lskeepalived.conf[devopsserver1 keepalived]$cpkeepalived.conf /home/devops/ansible/[devopsserver1 keepalived]$cd/home/devops/ansible/[devopsserver1 ansible]$lsapache.yml keepalived.conf keepalived.yml[devopsserver1 ansible]$mvkeepalived.conf keepalived.conf.j2[devopsserver1 ansible]$vimkeepalived.conf.j2[devopsserver1 ansible]$sudoansible-playbook keepalived.yml! Configuration File for keepalivedglobal_defs {router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0}vrrp_instance VI_1 {state {{ state }}interface {{ interface }}virtual_router_id {{ router_id }}priority {{ pri }}advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {{{ vip }}}}运行[devopsserver1 ansible]$ ansible-playbook keepalived.yml测试[devopsserver1 ansible]$ipa示例——批量创建用户[devopsserver1 ansible]$vimuser.yml --- - hosts: db tasks: - name: Add the user ansible.builtin.user: name:{{ item.user }}password:{{ item.pass | password_hash(sha512) }}state: present loop: -{user: user1, pass: pass1}-{user: user2, pass: pass2}运行[devopsserver1 ansible]$ ansible-playbook user.yml[rootnode1 ~]# cat /etc/shadow示例——结合haproxy[devopsserver1 ansible]$ vim hosts[devopsserver1 ansible]$ vim haproxy.yml --- - hosts: haproxy,webservers become: yes tasks: - name: deploy haproxy block: - name: Install the haproxy ansible.builtin.yum: name: haproxy state: present - name: configure the haproxy ansible.builtin.template: src: haproxy.cfg.j2 dest: /etc/haproxy/haproxy.cfg notify: restart service haproxy - name: Start service haproxy ansible.builtin.service: name: haproxy state: started enabled: yes when: ansible_hostname server1 - name: deploy apache block: - name: Install the Apache ansible.builtin.yum: name: httpd state: present - name: Start service httpd ansible.builtin.service: name: httpd state: started enabled: yes - name: creat index.html ansible.builtin.copy: content: {{ ansible_hostname }}\n dest: /var/www/html/index.html - name: Ensure the default Apaceh port is {{ http_port }} ansible.builtin.template: src: httpd.conf.j2 dest: /etc/httpd/conf/httpd.conf notify: restart service httpd when: ansible_hostname in groups[webservers] handlers: - name: restart service haproxy ansible.builtin.service: name: haproxy state: restarted - name: restart service httpd ansible.builtin.service: name: httpd state: restarted运行测试[devopsserver1 ansible]$ ansible-playbook haproxy.yml
返回列表