Ansible安装配置和基本使用

Ansible安装配置和基本使用。小编来告诉你更多相关信息。Ansible安装配置和基本使用为网友解答Ansible安装配置和基本使用方面的内容,具体内容如下:一、Ansible安装Ansibl

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

为网友解答Ansible安装配置和基本使用方面的内容,具体内容如下:

一、Ansible安装

Ansible的安装方法主要有以下四种:

1.1 yum使用EPEL源安装

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backupmv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backupwget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repoyum clean all && yum makecache# 可以使用以下三种方式查看ansible包的信息yum info ansibleyum list ansibleyum list | grep ansibleansible.noarch                                                      2.9.27-1.el7                                                  epelansible-collection-microsoft-sql.noarch                             1.1.0-1.el8                                                   AppStreamansible-collection-redhat-rhel_mgmt.noarch                          1.0.0-2.el8                                                   AppStreamansible-doc.noarch                                                  2.9.27-1.el7                                                  epelansible-freeipa.noarch                                              0.3.8-1.el8                                                   AppStreamansible-freeipa-tests.noarch                                        0.3.8-1.el8                                                   AppStreamansible-inventory-grapher.noarch                                    2.4.4-1.el7                                                   epelansible-lint.noarch                                                 3.5.1-1.el7                                                   epelansible-openstack-modules.noarch                                    0-20140902git79d751a.el7                                      epelansible-pcp.noarch                                                  2.2.1-1.el8                                                   AppStreamansible-python3.noarch                                              2.9.27-1.el7                                                  epelansible-review.noarch                                               0.13.4-1.el7                                                  epelansible-test.noarch                                                 2.9.27-1.el7                                                  epelcentos-release-ansible-29.noarch                                    1-2.el8                                                       extraskubernetes-ansible.noarch                                           0.6.0-0.1.gitd65ebd5.el7                                      epelpython2-ansible-runner.noarch                                       1.0.1-1.el7                                                   epelpython2-ansible-tower-cli.noarch                                    3.3.9-1.el7                                                   epelvim-ansible.noarch                                                  3.2-1.el7# 安装ansibleyum -y install ansibleansible --version

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

1.2 编译安装

yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto# 需要提前安装Python环境(略)wget https://releases.ansible.com/ansible/ansible-2.9.27.tar.gztar xf ansible-2.9.27.tar.gzcd ansible-2.9.27ls -altotal 160drwxr-xr-x  2 root root  4096 May 25 05:18 bindrwxr-xr-x  2 root root  4096 May 25 05:18 changelogsdrwxr-xr-x  3 root root  4096 May 25 05:18 contrib-rw-r--r--  1 root root 35148 May 25 05:18 COPYINGdrwxr-xr-x  6 root root  4096 May 25 05:18 docsdrwxr-xr-x  3 root root  4096 May 25 05:18 examplesdrwxr-xr-x  3 root root  4096 May 25 05:18 hackingdrwxr-xr-x  3 root root  4096 May 25 05:18 libdrwxr-xr-x  2 root root  4096 May 25 05:18 licenses-rw-r--r--  1 root root 13840 May 25 05:18 Makefile-rw-r--r--  1 root root  1731 May 25 05:18 MANIFEST.indrwxr-xr-x 10 root root  4096 May 25 05:18 packaging-rw-r--r--  1 root root  7724 May 25 05:18 PKG-INFO-rw-r--r--  1 root root  5175 May 25 05:18 README.rst-rw-r--r--  1 root root   351 May 25 05:18 requirements.txt-rw-r--r--  1 root root 12949 May 25 05:18 setup.py-rw-r--r--  1 root root 28352 May 25 05:18 SYMLINK_CACHE.jsondrwxr-xr-x  7 root root  4096 May 25 05:18 testpython setup.py buildpython setup.py installmkdir /etc/ansiblecp -r examples/* /etc/ansible

1.3 Git 源码安装

git clone https://github.com/ansible/ansible.gitcd ansiblegit checkout stable-2.9Updating files: 100% (19674/19674), done.Branch \'stable-2.9\' set up to track remote branch \'stable-2.9\' from \'origin\'.Switched to a new branch \'stable-2.9\'source ./hacking/env-setup

1.4 pip安装

yum install python-pip python-develyum install gcc glibc-devel zibl-devel rpm-bulid openss1-devel pip install -U pip pip install ansible --upgrade

Ansible安装配置和基本使用

二、Ansible相关文件说明

2.1 配置文件

  • /etc/ansible/ansible.cfg:主配置文件,配置ansible工作特性;
  • /etc/ansible/hosts:主机清单文件,管理的目标主机地址清单;
  • /etc/ansible/roles/: 存放角色的目录。

2.2 主配置文件介绍

[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模块

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

以上只是一小部分关于default的相关配置,还有其他更多的配置这里暂时不一一列举。因为ansible的大部分参数都可以保持默认,无需更改。但是当你的被管理机器数量增加以后,建议将forks数量适当的调整。

温馨提示:由于Ansible不是一个服务,所以更改完配置后无无需进行重启操作(也没提供重启的操作方法),改完配置立即生效。

2.3 Inventory主机清单文件

主机清单文件,用大白话讲其实就是被整合在一个文件中的一组或者多组被管控的节点。默认情况下,该清单文件为:/etc/ansible/hosts,通常我们可能会根据项目需求,起一个见名知意的其他名称,或者也会直接在每一套我们需要组织的项目根目录下存放一个hosts的清单文件。

Inventory主机清单文件可以有多个,在使用plyabook时可以手动通过-i参数指定你需要使用的清单文件名称。

Inventory文件遵循了ini配置文件的风格,一个section就表示一组隶属于同一个分组的被管理机器。目标被管理节点默认均被视为使用SSH的22端口,但是当部分节点使用非22端口号时,也可以在清单文件中进行指定。

被管理节点可以是以IP的形式写在清单文件中,也可以使用域名的形式。

[webservers]172.16.0.10172.16.0.11172.16.0.[20:30]alpha.ayunw.cnbeta.ayunw.cnapache[10:20].ayunw.cnnginx.ayunw.cn:2333[dbservers]db01.ayunw.cndb02.ayunw.cnmysql-[a:z].ayunw.cn10.25.1.5610.25.1.57

三、实战演练

3.1 环境介绍

我这里包括ansible主控机和另外四台被管理机器,以下是我自定义的一个Inventory文件,可以作为备忘用。

mkdir ansible-examplevim hosts[ayunw]10.10.108.3410.10.108.3010.10.108.3110.10.108.3210.10.108.33

以下是在ansible的默认hosts配置文件中配置,如果不使用 -i ./ansible-example/hosts,则ansible默认使用该清单文件。

[root@ansible-server ~]# cat /etc/ansible/hosts[websrvs]10.10.108.[30:33][dbsrvs]10.10.108.30[appsrvs]10.10.108.[30:33]

3.2 Ansible相关命令工具

  • /usr/bin/ansible:主提序,临时命令执行工具
  • /usr/bin/ansible-doc: 查看配置文档,模块功能查看工具
  • /us/bin/ansible-galaxy:下载/上传优秀代码或Roles模块的自网平台
  • /usr/bin/ansible-playbook:定制自动化任务,编排剧本工具
  • /usr/bin/ansible-pull:远程执行命令的工具
  • /usr/bin/ansible-vault:文件加密工具
  • /usr/bin/ansible-console:基于Console界面与用户交互的执行工具

以上命令,最常用的两个:ansibleansible-playbook

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

,这两个命令就是ansible用来实现批量管里的关键。

  • ansible命令通常用于一次性或者临时的任务,一般一条命令即可实现任务。通常我们称这种方式为:ad-hoc;
  • ansible-playbook主要是应用于针对大型项目的部署,需要通过多个yaml格式的文件(以yaml或者yml结尾)组合使用,所以一般需要进行提前规划目录,整合yaml文件。

接下来介绍几个比较常用的命令。

3.2.1 ansible-doc

该命令主要用于显示针对某个模块的使用方法的帮助信息。如果忘记了模块或者模块的用法,可以通过该命令快速查看。

ansible-doc [options][module...]# 列出可用模块-l,--list# 显示指定模块的playbook片段-s,--snippet如:# 列出所有模块ansible-doc -l # 查看指定模块帮助用法ansible-doc copy # 查看指定模块帮助用法[简化版的帮助]ansible-doc -s copy

注意:在使用ansible批量管理操作之前,需要先对所有主机做一个免密认证,以确保每个被管理节点都能在ansible管理节点上通过ssh协议免密登录到被管理节点。

免密的三种实现方式:

  • 基于ssh-keygen + shell + sshpass`方法实现
  • 基于expect实现;
  • 基于ansible-playbook实现。

ad-hoc 方式常用参数说明:

  • --version:显示版本
  • -m module:指定模块,默认为command
  • -V:详细过程-vv -vvv更详细
  • --list-hosts:显示主机列表,可简写–list
  • -k,--ask-pass:提示输入ssh连接密码,默认key验证
  • -C,--check:检查,并不执行
  • -T,--timeout=TIMEOUT:执行命令的超时时间,默认10s
  • -u,--user=REMOTE_USER:执行远程执行的用户
  • -b,--become:代替旧版的sudo切换
  • --become-user=USERNAME:指定sudo的runas用户,默认为root
  • -K,--ask-become-pass:提示输入sudo时的口令

使用 * 通配符

# 以下两条命令执行效果相同ansible all -m pingansible \"*\" -m pingansible 10.10.108.* -m pingansible \"*srvs\" -m ping [root@ansible-srv1 ansible-example]# ansible \"*\" --list-hosts  hosts (4):    10.10.108.30    10.10.108.31    10.10.108.32    10.10.108.33

使用逻辑或

[root@ansible-srv1 ansible-example]# ansible \"websrvs:appsrvs\" -m ping10.10.108.31 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.32 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.33 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.30 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}[root@ansible-srv1 ansible-example]# ansible \"10.10.108.30:10.10.108.33\" -m ping10.10.108.30 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.33 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

使用逻辑与
在websrvs组并且在dbsrvs组中的主机。

[root@ansible-srv1 ansible-example]# ansible \"websrvs:&dbsrvs\" -m ping10.10.108.30 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}

使用逻辑非
在websrvs组,但不在dbsrvs组中的主机.注意:此处只能是单引号。

[root@ansible-srv1 ansible-example]# ansible \'websrvs:!dbsrvs\' -m ping10.10.108.33 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.31 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.32 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}

使用正则表达式

[root@ansible-srv1 ansible-example]# ansible \"websrvs:&dbsrvs\" -m ping10.10.108.30 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}[root@ansible-srv1 ansible-example]# ansible \"~(web|db)srvs\" -m ping10.10.108.30 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.33 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.31 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}10.10.108.32 | SUCCESS => {    \"ansible_facts\": {        \"discovered_interpreter_python\": \"/usr/bin/python\"    },    \"changed\": false,    \"ping\": \"pong\"}

3.2.2 ansible执行命令过程

  • 加载自己的配置文件默认/etc/ansible/ansible.cfg,如果指定了你自定义的清单文件,则从自己的清单文件中查找被管理主机
  • 加载自己对应的模块文件,如:command
  • 通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
  • 给文件+x执行
  • 执行并返回结果
  • 删除临时py文件,退出

可以通过加参数-v或者-vvv列出详细的执行过程(可以多加几个v参数)。

[root@ansible-srv1 ansible-example]# ansible \"~(web|db)servers\" -vvv -m ping > ansible.log[root@ansible-srv1 ansible-example]# grep \"chmod\" ansible.log

3.2.3 ansible执行后颜色描述

默认情况下是以下三种颜色:

Ansible安装配置和基本使用。小编来告诉你更多相关信息。

Ansible安装配置和基本使用

  • 绿色:表示成功
  • 黄色:修改了远程文件后并执行成功
  • 红色:表示执行失败

但是在ansible配置文件中可以定义颜色,如下:

[root@ansible-srv1 ansible-example]# vim /etc/ansible/ansible.cfg[colors]#highlight = white#verbose = blue#warn = bright purple#error = red#debug = dark gray#deprecate = purple#skip = cyan#unreachable = red#ok = green#changed = yellow#diff_add = green#diff_remove = red#diff_lines = cyan

3.2.4 Ansible-playbook示例

[root@ansible-srv1 ansible-example]# cat echo-demo.yml---- hosts: dbsrvs  remote_user: root  tasks:    - name: echo demo      shell: echo \"第一个 ansible-playbook 示例\" [root@ansible-srv1 ansible-example]# ansible-playbook echo-demo.yml

解释:这里指定主机组为:dbsrvs,指定远程用户为root,使用shell模块执行了一个echo命令。

3.2.5 ansible-vault

该工具用于对yaml文件进行加解密,格式如下:

ansible-vault [create|decrypt|edit|encrypt|rekey|view]如:ansible-vault encrypt echo-demo.yml# 加密ansible-vault view echo-demo.yml  # 查看ansible-vault decrypt echo-demo.yml# 解密ansible-vault edit echo-demo.yml  # 编辑加密文件ansible-vault create echo-demo.yml# 创建新文件

Ansible是一个简单高效且很强大的工具,它的功能远不止于此。而那些不常用的命令工具,本文也暂不介绍,学习了不用也是在浪费自己的时间。如果有兴趣的话可以去官网或者其他站点查阅资料自行了解。

以上就是带来的Ansible安装配置和基本使用的全部内容,您了解了吗?

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 5733401@qq.com 举报,一经查实,本站将立刻删除。本文链接:https://www.fajihao.com/i/242473.html

(0)
恰卡的头像恰卡
上一篇 2023-09-21
下一篇 2023-09-21

相关推荐