Ansible安装配置和基本使用

一、Ansible安装

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

1.1 yum使用EPEL源安装

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache

# 可以使用以下三种方式查看ansible包的信息
yum info ansible
yum list ansible
yum list | grep ansible
ansible.noarch                                                      2.9.27-1.el7                                                  epel
ansible-collection-microsoft-sql.noarch                             1.1.0-1.el8                                                   AppStream
ansible-collection-redhat-rhel_mgmt.noarch                          1.0.0-2.el8                                                   AppStream
ansible-doc.noarch                                                  2.9.27-1.el7                                                  epel
ansible-freeipa.noarch                                              0.3.8-1.el8                                                   AppStream
ansible-freeipa-tests.noarch                                        0.3.8-1.el8                                                   AppStream
ansible-inventory-grapher.noarch                                    2.4.4-1.el7                                                   epel
ansible-lint.noarch                                                 3.5.1-1.el7                                                   epel
ansible-openstack-modules.noarch                                    0-20140902git79d751a.el7                                      epel
ansible-pcp.noarch                                                  2.2.1-1.el8                                                   AppStream
ansible-python3.noarch                                              2.9.27-1.el7                                                  epel
ansible-review.noarch                                               0.13.4-1.el7                                                  epel
ansible-test.noarch                                                 2.9.27-1.el7                                                  epel
centos-release-ansible-29.noarch                                    1-2.el8                                                       extras
kubernetes-ansible.noarch                                           0.6.0-0.1.gitd65ebd5.el7                                      epel
python2-ansible-runner.noarch                                       1.0.1-1.el7                                                   epel
python2-ansible-tower-cli.noarch                                    3.3.9-1.el7                                                   epel
vim-ansible.noarch                                                  3.2-1.el7

# 安装ansible
yum -y install ansible
ansible --version

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.gz
tar xf ansible-2.9.27.tar.gz
cd ansible-2.9.27
ls -al
total 160
drwxr-xr-x  2 root root  4096 May 25 05:18 bin
drwxr-xr-x  2 root root  4096 May 25 05:18 changelogs
drwxr-xr-x  3 root root  4096 May 25 05:18 contrib
-rw-r--r--  1 root root 35148 May 25 05:18 COPYING
drwxr-xr-x  6 root root  4096 May 25 05:18 docs
drwxr-xr-x  3 root root  4096 May 25 05:18 examples
drwxr-xr-x  3 root root  4096 May 25 05:18 hacking
drwxr-xr-x  3 root root  4096 May 25 05:18 lib
drwxr-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.in
drwxr-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.json
drwxr-xr-x  7 root root  4096 May 25 05:18 test
python setup.py build
python setup.py install

mkdir /etc/ansible
cp -r examples/* /etc/ansible

1.3 Git 源码安装

git clone https://github.com/ansible/ansible.git
cd ansible
git checkout stable-2.9
Updating 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-devel
yum install gcc glibc-devel zibl-devel rpm-bulid openss1-devel 
pip install -U pip 
pip install ansible --upgrade
图片[1]-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模块

以上只是一小部分关于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.10
172.16.0.11
172.16.0.[20:30]
alpha.ayunw.cn
beta.ayunw.cn
apache[10:20].ayunw.cn
nginx.ayunw.cn:2333

[dbservers]
db01.ayunw.cn
db02.ayunw.cn
mysql-[a:z].ayunw.cn
10.25.1.56
10.25.1.57

三、实战演练

3.1 环境介绍

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

mkdir ansible-example
vim hosts
[ayunw]
10.10.108.34

10.10.108.30
10.10.108.31
10.10.108.32
10.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命令通常用于一次性或者临时的任务,一般一条命令即可实现任务。通常我们称这种方式为: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 ping
ansible "*" -m ping

ansible 10.10.108.* -m ping
ansible "*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 ping
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"
}
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 ping
10.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"
}

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

[root@ansible-srv1 ansible-example]# ansible "websrvs:&dbsrvs" -m ping
10.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 ping
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"
}

 
使用正则表达式

[root@ansible-srv1 ansible-example]# ansible "websrvs:&dbsrvs" -m ping
10.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 ping
10.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配置文件中可以定义颜色,如下:

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

© 版权声明
THE END
喜欢就支持一下吧
点赞126赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容