架构篇--8--ansible的安装与使用

    科技2025-09-28  47

    ansible的安装与使用

    Ansible概述:

    ​ 是一个配置管理系统(configuration management system),当下最流行的批量自动化运维工具之一. ​ 常用的运维工具:ssh/puppet(ruby)/ansible(无客户端,中小规模)(python)/saltstack(master-minion)(python)大规模 Ansible的作用: ​ 批量部署,服务安装,日常备份 Ansible官方文档: ​ https://docs.ansible.com/ansible/latest/index.html Ansible的特性: ​ 无客户端软件,通过ssh远程管理 ​ 安装后不需要启动服务 ​ 依赖大量的Python模块扩展功能 ​ 配置文件:/etc/ansible/ansible.cfg Ansible基础架构: ​ 连接插件(connecter plugins):用来连接主机,连接被管理端 ​ 核心模块(core modules):连接主机,实现操作,依赖于具体模块来执行 ​ 自定义模块:用户自己开发的功能模块 ​ 剧本(playbook):将多个任务组合成一个剧本,由ansible自动批量执行 ​ 主机清单(host inventory):定义ansible管理的客户端主机范围 Ansible的命令格式: ​ ansible 主机清单名 -m 调用的模块 -a 动作命令

    ----------------------------------------------------使用----------------------

    1.安装ansible

    (1)配置epel源
    epel源(扩展包):wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo linux镜像源(组包):wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    (2)yum安装
    yum -y install ansible ansible --version ##查看版本
    (3)配置主机清单
    vim /etc/ansible/hosts 添加: [web] ##主机清单名 192.168.1.50 ##主机IP地址 可同时添加多个 [rsync] 192.168.1.40 保存退出
    (4)配置ssh密钥对访问 (如有多台都进行此操作!!)
    [root@localhost ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 85:5b:6f:c7:a5:36:8c:70:1b:bf:fa:b0:7e:9c:66:f1 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | | | . | | . + o .| | + + B o | | S = O | | . o.o | | ...+ | | +* E| | .+=. | +-----------------+ [root@localhost ~]# ssh-copy-id root@192.168.1.50 The authenticity of host '192.168.1.50 (192.168.1.50)' can't be established. ECDSA key fingerprint is b5:60:c3:7a:34:c5:7c:f3:fe:4f:a5:07:88:55:c1:3e. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed 123^H^H^H/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.1.50's password: ##输入root密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.1.50'" and check to make sure that only the key(s) you wanted were added. [root@localhost conf]# ssh-copy-id root@192.168.1.10 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.1.10's password: ##输入root密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.1.10'" and check to make sure that only the key(s) you wanted were added.

    2.Ansible的基础应用

    (1)调用模块颜色显示

    翔黄色 更改成功 帽绿色 没有更改 深红色 错误 亮紫色 警告

    (2) 各个模块的应用

    列出所有模块 ansible-doc --list

    command模块 #仅支持简单语法命令,但语句中不能包含管道符等复杂元素 [root@localhost ~]# ansible rsync -m command -a "hostname" 192.168.1.50 | CHANGED | rc=0 >> mysql [root@localhost ~]# ansible rsync -m command -a "useradd zhangsan" 192.168.1.50 | CHANGED | rc=0 >> shell #command升级版,支持复杂语句,但不支持别名 [root@localhost ~]# ansible rsync -m shell -a "echo 123 >/root/aaa.txt | cat /root/aaa.txt" 192.168.1.50 | CHANGED | rc=0 >> 123 yum ##可以帮助我们在远程主机上通过 yum 源管理软件包 [root@localhost ~]# ansible rsync -m yum -a "name=httpd state=installed" ##安装httpd 192.168.1.50 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "httpd" ] }, ············································显示报黄操作成功 [root@localhost ~]# ansible rsync -m yum -a "name=httpd state=removed" ##卸载httpd 192.168.1.50 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "removed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ "已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 httpd.x86_64.0.2.4.6-93.el7.centos 将被 删除\n--> 解 ·····················显示报黄操作成功 注释:name 安装的软件包名,多个软件用","分开 state 服务状态 installed,present 安装软件包 removed,absent 卸载软件包 latest 安装最新软件包 disable_gpg_check=no|yes: 禁用对rpm包的公钥验证,默认no; copy ##作用就是拷贝文件 ,copy 模块是将 ansible 管理主机上的文件拷贝到远程主机中 [root@localhost ~]# ansible rsync -m copy -a "src=/root/aaaa.txt dest=/root/ backup=yes" 192.168.1.50 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/root/aaaa.txt", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:admin_home_t:s0", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1601293999.86-48984-12050110487624/source", "state": "file", "uid": 0 }······································显示报黄操作成功--可在对应主机下进行验证 注释: src 源文件路径 dest 目标文件路径--必须参数 backup 覆盖到目标文件前,是否提前备份 content 可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错。 group 指定属组,远程主机上必须有对应的组,否则会报错 owner 指定属主,远程主机上必须有对应的用户,否则会报错 mode 指定文件拷贝后的权限,想将权限设置为”rw-r--r--“,则可以使用mode=0644表示,如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示。 force 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no service ##管理远程主机上的服务 [root@localhost ~]# ansible web -m service -a "name=httpd state=started enabled=yes" 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "enabled": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0"······························显示报黄操作成功 注释: name 指定服务名 state 指定服务运行状态 started 开启服务 stopped 关闭服务 reloaded 重载服务 restarted 重启服务 enabled 是否开机自启 group ##用来添加或者删除组 [root@localhost ~]# ansible all -m group -a "name=www gid=666" ##在所有清单主机添加www组gid666 The authenticity of host '192.168.1.40 (192.168.1.40)' can't be established. ECDSA key fingerprint is f8:db:9c:59:09:fe:17:6f:e7:d3:d0:84:50:72:f6:21. Are you sure you want to continue connecting (yes/no)? yes 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 666, "name": "www", "state": "present", "system": false 192.168.1.40 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 666, "name": "www", "state": "present", "system": false } [root@localhost ~]# ansible all -m group -a "name=www gid=666 state=absent" ##删除所有清单的www组 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "www", "state": "absent" } 192.168.1.40 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "www", "state": "absent" } 注释 name 必须参数,用于指定要操作的组名称。 state 用于指定组的状态,两个值可选,present,absent,默认为 present,设置为absent 表示删除组。 gid 用于指定组的gid。 user ##帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作。 [root@localhost ~]# ansible all -m user -a "name=www" ##在所有清单主机创建www用户 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/www", "name": "www", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } 192.168.1.40 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/www", "name": "www", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } [root@localhost ~]# ansible web -m shell -a "echo 123 |passwd --stdin www" ##设置web上www用户密码 192.168.1.10 | CHANGED | rc=0 >> 更改用户 www 的密码 。 passwd:所有的身份验证令牌已经成功更新。 注释: name 必须参数,用于指定要操作的用户名称,可以使用别名 user group 此参数用于指定用户所在的基本组。 gourps 此参数用于指定用户所在的附加组 shell 此参数用于指定用户的默认 shell。 uid 此参数用于指定用户的 uid 号。 comment 此参数用于指定用户的注释信息。 remove 当 state 的值设置为 absent 时,表示要删除远程主机中的用户 password 此参数用于指定用户的密码。但是这个密码不能是明文的密码,而是一个对明文密码”加密后”的字符串,相当于 /etc/shadow 文件中的密码字段,是一个对明文密码进行哈希后的字符串,你可以在 python 的命令提示符下输入如下命令,生成明文密码对应的加密字符串。 file ##可以帮助我们完成一些对文件的基本操作,创建文件或目录、删除文件或目录、修改文件权限 [root@localhost ~]# ansible rsync -m file -a "path=/backup owner=root group=root recurse=yes mode=777" ##在rsync创建backup目录 属主属组为root 权限为777 开启递归 192.168.1.40 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/backup", "secontext": "unconfined_u:object_r:default_t:s0", "size": 6, "state": "directory", "uid": 0 } [root@localhost ~]# ansible rsync -m file -a "path=/test.txt owner=root group=root state=touch mode=777" ##创建一个文件为test.txt 属主属组为root 权限为777 state指定为touch 192.168.1.40 | CHANGED => "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/test.txt", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:etc_runtime_t:s0", "size": 0, "state": "file", "uid": 0 } 注释: path 指定操作的文件目录,为了兼容之前的版本,使用dest或name也可以。 state 灵活操作,创建目录指定为directory 创建文件指定为touch 创建软连接指定为link 创建硬链接指定为hard 删除文件指定为absent src 当state设置为link或者hard时,表示我们想要创建一个软链或者硬链,所以,我们必须指明软链或硬链链接的哪个文件,通过src参数即可指定链接源 force 当state=link的时候,可配合此参数强制创建链接文件,当force=yes时,表示强制创建链接文件。不过强制创建链接文件分为三种情况。{情况一:当要创建的链接文件指向的源文件并不存在时,使用此参数,可以先强制创建出链接文件。 情况二:当要创建链接文件的目录中已经存在与链接文件同名的文件时,将force设置为yes,会将同名文件覆盖为链接文件,相当于删除同名文件,创建链接文件。 情况三:当要创建链接文件的目录中已经存在与链接文件同名的文件,并且链接文件指向的源文件也不存在,这时会强制替换同名文件为链接文件。} owner 用于指定被操作文件的属主 group 用于指定被操作文件的属组 mode 用于指定文件/目录,参数 recurse 当要操作的文件为目录,将recurse设置为yes,可以递归的修改目录中文件的属性 mount ##用于批量管理主机进行挂载卸载操作 ------案列-nfs挂载----------(我们用web主机下载nfs rpcbind代替nfs共享服务器 步骤省) [root@localhost ~]# ansible web -m file -a "path=/nfs owner=root group=root recurse=yes mode=777" ##创建nfs挂载目录 属主属组为root 权限为777 开启递归 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0777", "owner": "root", "path": "/nfs", "size": 6, "state": "directory", "uid": 0 }································显示报黄 操作成功 [root@localhost ~]# ansible web -m copy -a "src=exports dest=/etc/exports" ##将本地的export 复制到nfs下 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "57079785c115434a677de60adf6a659be39b5631", "dest": "/etc/exports", "gid": 0, "group": "root", "md5sum": "492abbb2711cbaf6d43492f854ab9b6c", "mode": "0644", "owner": "root", "size": 47, "src": "/root/.ansible/tmp/ansible-tmp-1601302969.91-52158-25793260336839/source", "state": "file", "uid": 0 }···························显示报黄 操作成功 [root@localhost ~]# ansible web -m service -a "name=nfs state=restarted" ##启动nfs 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "nfs", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", "After": "local-fs.target ··························显示报黄 操作成功 [root@localhost ~]# ansible web -m service -a "name=rpcbind state=restarted" ##启动rpcbind服务 192.168.1.10 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "rpcbind", "state": "started", "status": { "ActiveEnterTimestamp": "一 2020-09-28 22:24:55 CST", "ActiveEnterTimestampMonotonic": "7263065427", "ActiveExitTimestampMonotonic": "0", [root@localhost ~]# ansible rsync -m mount -a "src=192.168.1.10:/nfs path=/backup fstype=nfs state=mounted" ##将共享目录挂载到 rsync下的backup (backup我们在此之前创建过) 192.168.1.40 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dump": "0", "fstab": "/etc/fstab", "fstype": "nfs", "name": "/backup", "opts": "defaults", "passno": "0", "src": "192.168.1.10:/nfs" }·····························显示报黄 操作成功 注释: fstype 指定挂载的文件系统类型 opts 指定挂载的文件系统类型 path 挂载到的目标路径 src 挂载的原路径 state absent 会进行卸载,也会修改fstab文件信息 present 不会挂载,只会修改fstab文件 unmounted 会进行卸载,不会修改fstab文件 mounted 会进行挂载,会修改fstab文件 script ##可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。 编写一个简单脚本 [root@localhost ~]# cat /testdir/test.sh #!/bin/bash echo 'testaaaaa' > index.html [root@localhost ~]# ansible web -m script -a "chdir=/opt /testdir/test.sh" ##运行脚本 到web主机下的opt目录中 192.168.1.10 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.10 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.1.10 closed." ], "stdout": "", "stdout_lines": [] } [root@localhost ~]# ansible web -m shell -a "cat /opt/index.html" ##验证是否运行 192.168.1.10 | CHANGED | rc=0 >> testaaaaa 注释 chdir 指定远程主机的目录,然后进行操作
    (3)ansible playbook-----ymal

    组织多个task的容器有着特定的组织格式,由一个或多个模块组成,完成统一的目的,实现自动化操作.它采用的语法格式是YAML。YAML语法能够简单的表示散列表,字典等数据结构。

    基础组件

    Hosts:运行执行任务(task)的目标主机remote_user:在远程主机上执行任务的用户tasks:任务列表handlers:任务,与tasks不同的是只有在接受到通知时才会被触发templates:使用模板语言的文本文件,使用jinja2语法。variables:变量,变量替换{{ variable_name }}

    yaml的三要素: 冒号:冒号后面需要空格,除非以冒号结尾 短横杠:列表项,后面跟空格yaml的三要素: 缩进:两个字符,默认的tab键是四个字符,所以要使用tab键,需要修改.vimrc

    vim /root/.vimrc ##设置tab键额缩进格式 添加: set tabstop=2 保存退出

    ----------练习案列--------playbook安装httpd并修改端口为8080----访问测试

    ​ 前提: 安装httpd , 复制httpd.conf到当前编写脚本目录 ,修改端口8080

    [root@localhost ~]# mkdir -p /jiaoben ##创建存放脚本的目录 [root@localhost ~]# cd /jiaoben ##进入到当前目录 [root@localhost jiaoben]# vim httpd.conf Listen 8080 ##将端口修改为8080 [root@localhost jiaoben]# vim httpd.ymal ##编写ymal脚本 - hosts: web ##操作的主机 tasks: ##下面构成playbook的tasks - name: install httpd ##指定该任务的名称。 yum: name=httpd state=latest ##调用yum模块 参数说明上看 - name: httpd config copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf notify: restart httpd - name: start httpd service: name=httpd state=started handlers: - name: restart httpd service: name=httpd state=restarted 保存退出 注释: 上述命令参考模块介绍即可! Handlers介绍: andlers也是一些task的列表,和一般的task并没有什么区别。 是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行 不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次 [root@localhost jiaoben]# ansible-playbook -C httpd.ymal ##测试语法是否正确 [root@localhost jiaoben]# ansible-playbook httpd.ymal ##执行此脚本

    注释:下面图片为测试式的参数说明(https://blog.51cto.com/13589448/2068546)

    Processed: 0.012, SQL: 8