一、Ansible简介

  Ansible创立于2012年2月,一个基于Python开发的轻量级自动化运维工具,有着其他自动化运维工具如puppet、cfengine、chef、func、fabric的优点,并且不需要单独安装客户端以及启动服务,只需要通过SSH就可以快速的对大量客户端实现批量系统配置、程序部署、批量运行命令等。Ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。
  主要包括:

  • 连接插件connection plugins:负责和被监控端实现通信;
  • host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
  • 各种模块核心模块、command模块、自定义模块;
  • 借助于插件完成记录日志邮件等功能;
  • playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

  Ansible的主要优势:

  • (1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
  • (2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
  • (3)、使用python编写,维护更简单;
  • (4)、支持sudo。

二、安装步骤

0、环境说明

操作系统版本为centos7.6
[root@test1 scripts]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

1、安装YUM epel扩展源

[root@test1 yum.repos.d]# yum install epel-release -y

2、yum安装ansible

[root@test1 yum.repos.d]# yum install ansible -y

Installed:
ansible.noarch 0:2.9.18-1.el7

Dependency Installed:
python-babel.noarch 0:0.9.6-8.el7 python-jinja2.noarch 0:2.7.2-4.el7 python-markupsafe.x86_64 0:0.11-10.el7
python-paramiko.noarch 0:2.1.1-9.el7 python2-httplib2.noarch 0:0.18.1-3.el7 python2-jmespath.noarch 0:0.9.4-2.el7
sshpass.x86_64 0:1.06-2.el7

Complete!

3、查看ansible版本

[root@test1 yum.repos.d]# ansible --version
ansible 2.9.18
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

4、配置主机列表清单

[root@test1 ansible]# vim /etc/ansible/hosts
[test]
test1 ansible_ssh_host=192.168.0.124 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=“123456”
test2 ansible_ssh_host=192.168.0.125 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=“123456”
test3 ansible_ssh_host=192.168.0.126 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=“123456”

5、配置ansible主配置文件

[root@test1 ansible]# vim /etc/ansible/ansible.cfg
这是ansible的主配置文件,用于定义并发数、主机列表默认位置、远程主机默认端口等等。此示例只去掉host_key_checking = False的注释符#,其他暂时不做修改调整,使用默认配置。

windows11 ansible安装 ansible安装软件_windows11 ansible安装

6、使用验证

  • ping TEST分组主机

[root@test1 ansible]# ansible test -m ping
192.168.0.125 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
192.168.0.126 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}
192.168.0.124 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
}

  • 查看test1主机的磁盘利用率

[root@test1 ansible]# ansible test1 -m shell -a “df -hT”
test1 | CHANGED | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 38G 4.4G 34G 12% /
devtmpfs devtmpfs 471M 0 471M 0% /dev
tmpfs tmpfs 487M 124K 487M 1% /dev/shm
tmpfs tmpfs 487M 15M 473M 3% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 147M 151M 50% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0

  • 查看主机test1和test2的环境变量

[root@test1 ansible]# ansible test1,test2 -m shell -a “echo $PATH”
test2 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
test1 | CHANGED | rc=0 >>
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

三、FAQ

1、首次执行ansible命令时报错Using a SSH password instead of a key is not possible

  • 报错信息
  • windows11 ansible安装 ansible安装软件_Ansible_02

  • 报错原因
    ssh第一次连接的时候一般会提示输入yes进行确认为将key字符串加入到 ~/.ssh/known_hosts 文件中
  • 解决方案
    编辑ansible.conf配置文件,修改host_key_checking = False,去掉注释符号#
    [root@test1 ansible]# vim ansible.cfg
  • windows11 ansible安装 ansible安装软件_自动化运维_03