系列开篇说明
此次新开的一个自动化更新系列,主要是因为最近工作中用的比较多,而且Ansible在当今的生产环境中,对于运维来讲几乎是必备的技能。
同类型产品中除了Ansible外当然还有Puppet/Chef,但是相对于Ansible来说学习成本是比较高的,而且Ansible已经能够满足大部分的场景。
此系列博客不会从0开始写,而是多一些生产中的技巧及用法。对于0基础的同学来说,建议先去对Ansible有部分的了解再来看此系列博客。
此篇主要是介绍安装及简单使用。
正文
环境介绍
为了覆盖多种环境的情况,将会使用当前企业中较多使用的操作系统进行演示。
主机名 | 执行用户 | IP | OS | Python版本 | 备注 |
---|
ansible01 | ansible | 10.0.2.6/24 | Ubuntu 20.04 | 3.8.10 | 主控端 |
ansible02 | ansible | 10.0.2.7/24 | Ubuntu 20.04 | 3.8.10 | 被控端 |
ansible03 | ansible | 10.0.2.8/24 | Rocky Linux 8 | 3.6.8 | 被控端 |
ansible04 | ansible | 10.0.2.9/24 | CentOS 7 | 2.7.5 | 被控端 |
安装
Ansible有多个维护版本,一是各大Linux发行版自己维护的版本(下称Linux版本),二就是使用Ansible维护的官方版本(下称官方版本)。
对于大家看过的大部分教程来说,都是使用Linux版本来安装及使用。但是我个人其实是更推荐使用官方版本。
对于使用Linux版本来说,会生成一部分的配置文件,且目录比较规范,对于不太熟悉的人来说可能会省掉很多事情;当然缺点就是版本会落后于官方版本。
而官方版本不会生成Ansible的目录,不会生成配置文件,安装完需要自己来配置,对于部分人来说可能不太友好。
Linux版本安装
对于debian系的Linux来讲使用以下命令安装:
$ sudo apt-get install -y ansible
对于Redhat系的Linux来说使用以下命令安装:
yum install -y ansible
dnf install -y ansible
官方版本安装
因为Python的安装版本更加具备通用性,所以我个人更推荐使用Python安装的版本,此系列的教程也以Python的版本为准
不建议使用低于3.6.5版本的Python,因为3.6.5以前的大部分特性已被废弃
不建议直接使用操作系统中的Python来进行安装,操作系统中的默认Python应保持一个干净的环境。建议使用pyvenv来生成一个独立的Python环境来使用ansible
通过以下方式生成pyvenv
ansible@ansible01:~$ mkdir pyvenv
ansible@ansible01:~$ python3 -m venv pyvenv/ansible
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/ansible/pyvenv/ansible/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
ansible@ansible01:~$ sudo apt install -y python3.8-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libfwupdplugin1 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libpython3.8 libpython3.8-minimal libpython3.8-stdlib python-pip-whl python3.8 python3.8-minimal
Suggested packages:
python3.8-doc binutils binfmt-support
The following NEW packages will be installed:
python-pip-whl python3.8-venv
The following packages will be upgraded:
libpython3.8 libpython3.8-minimal libpython3.8-stdlib python3.8 python3.8-minimal
5 upgraded, 2 newly installed, 0 to remove and 35 not upgraded.
Need to get 8,115 kB of archives.
After this operation, 2,331 kB of additional disk space will be used.
Get:1 https://mirrors.bfsu.edu.cn/ubuntu focal-updates/main amd64 libpython3.8 amd64 3.8.10-0ubuntu1~20.04.6 [1,625 kB]
Get:2 https://mirrors.bfsu.edu.cn/ubuntu focal-updates/main amd64 python3.8 amd64 3.8.10-0ubuntu1~20.04.6 [387 kB]
······
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for mime-support (3.64ubuntu1) ...
ansible@ansible01:~$ python3 -m venv pyvenv/ansible
ansible@ansible01:~$ pyvenv/ansible/bin/python3 -V
Python 3.8.10
ansible@ansible01:~$ source pyvenv/ansible/bin/activate
(ansible) ansible@ansible01:~$
(ansible) ansible@ansible01:~$ pip install ansible
查看版本
(ansible) ansible@ansible01:~$ pip show ansible
Name: ansible
Version: 6.7.0
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /home/ansible/pyvenv/ansible/lib/python3.8/site-packages
Requires: ansible-core
Required-by:
基本概念介绍
安装完之后会有一系列的ansible命令,但是我仅介绍常用的命令。
$ ansible
$ ansible-config
$ ansible-doc
$ ansible-vault
$ ansible-galaxy
$ ansible-playbook
关于互信
对于大家看过的大部分教程来说,当然是使用ssh密钥对来实现Ansible主控端与被控端的互信。但是在一些企业中,可能没有root用户来直接操作(对,你没看错,对于一些管理严格的企业就算是运维也没有root用户的权限),并且可能由于使用了LDAP(或相似技术),也无法使用密钥对来做互信,所以Ansible也提供了使用账号密码的形式来执行操作。
关于inventory
以下为Ansible官方的描述:
Ansible automates tasks on managed nodes or “hosts” in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ansible users create inventory files. Your inventory defines the managed nodes you automate, with groups so you can run automation tasks on multiple hosts at the same time. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.
The simplest inventory is a single file with a list of hosts and groups. The default location for this file is /etc/ansible/hosts. You can specify a different inventory file at the command line using the -i
option or in configuration using inventory.
Ansible 使用称为清单的列表或列表组自动执行基础设施中托管节点或“主机”上的任务。 您可以在命令行中传递主机名,但大多数 Ansible 用户会创建清单文件。 您的清单定义了您自动化的托管节点和组,因此您可以同时在多个主机上运行自动化任务。 定义清单后,您可以使用模式来选择您希望 Ansible 运行的主机或组。
最简单的清单是一个包含主机和组列表的文件。 此文件的默认位置是 /etc/ansible/hosts。 您可以在命令行中使用 -i
选项或在配置中使用 inventory 指定不同的清单文件。
简单理解就是这个文件记录了需要执行操作的所有主机,并且可以通过分组来进行区分,且可以按照分组来执行任务,这个文件就是inventory。
比如说我在当前目录下的inventory文件
(ansible) ansible@ansible01:~$ cat hosts
[ansible02]
10.0.2.7
[ansible03]
10.0.2.8
[ansible04]
10.0.2.9
如果使用账号密码来进行操作ansible的被控端,需要安装sshpass包。
(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k -m ping
SSH password:
10.0.2.7 | FAILED! => {
"msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
10.0.2.8 | FAILED! => {
"msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
10.0.2.9 | FAILED! => {
"msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}
(ansible) ansible@ansible01:~$ sudo apt install -y sshpass
(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k -m ping
SSH password:
10.0.2.9 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
10.0.2.7 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
10.0.2.8 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
(ansible) ansible@ansible01:~$ ansible all -i hosts -u ansible -k --ssh-common-args='-o StrictHostKeyChecking=no' -m ping
SSH password:
10.0.2.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.0.2.8 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
10.0.2.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
总结
以上就是一次简单的ansible示范,主要是给部分没用过的同学来先熟悉以下ansible的用法,后续的博客将会循序渐进,介绍一些企业中更常用的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)