Ansible-base和Ansible之间的区别是什么?
首先
从Ansible 2.10开始,与传统的Ansible 2.9不同,大部分模块集中在ansible-galaxy中,并开始提供最基本的ansible-base模块。
然而,Ansible 2.10的安装方式有以下两种。
1. Ansible-base
只提供ansible-base,仅包含最基本的模块和插件,需要的模块可以从ansible-galaxy获取。
2. Ansible
与传统的分发形式相同,将模块和插件绑定到Ansible-base中。
安装 Ansible-base 和 Ansible,并验证它们之间的运行差异。
1. 自动化工具
1-1. 版本确认
确认是否已安装 Ansible 2.10。
# ansible --version
ansible 2.10.4
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Ansible-base是一个独立的包。
# pip3 freeze
ansible==2.10.4
ansible-base==2.10.4
・・・
1-2. 执行剧本
执行以下playbook。
※用于管理服务器(Ansible)的用户ted和被管理服务器的用户root之间进行密钥交换的内容。
---
- hosts: ap_servers
gather_facts: false
tasks:
- name: Set authorized key took from file
authorized_key:
user: root
state: present
key: "{{ lookup('file', '/home/ted/.ssh/id_rsa.pub') }}"
执行结果
执行结果如下。
$ ansible-playbook -i inventory playbook.v2.yml
PLAY [ap_servers] **************************************************************
TASK [Set authorized key took from file] ***************************************
ok: [192.168.11.21]
PLAY RECAP *********************************************************************
192.168.11.21 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. Ansible基础版本
2-1. 查阅版本信息
确认已经安装Ansible-base。
# ansible --version
ansible 2.10.3
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/python390/lib/python3.9/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.9.0 (default, Nov 30 2020, 16:14:32) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
只需要ansible-base软件包。
# pip3.9 freeze
ansible-base==2.10.3
・・・
运行2-2的战术手册。
执行以下的playbook。
请注意,在Ansible管理服务器用户为ted和管理目标服务器用户为root之间进行密钥交换。
---
- hosts: ap_servers
gather_facts: false
tasks:
- name: Set authorized key took from file
authorized_key:
user: root
state: present
key: "{{ lookup('file', '/home/ted/.ssh/id_rsa.pub') }}"
执行结果
执行结果如下。 模块不足,无法执行。
$ ansible-playbook -i inventory playbook.v2.yml
ERROR! couldn't resolve module/action 'authorized_key'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/home/ted/playbook.v2.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: Set authorized key took from file
^ here
2-3. 安装不足的模块(脱机安装)
安装资源以从 Ansible-galaxy 安装缺失的模块。
# ansible-galaxy collection download ansible.posix
Process install dependency map
Starting collection download process to '/root/collections'
Downloading collection 'ansible.posix' to '/root/collections/ansible-posix-1.1.1.tar.gz'
Downloading https://galaxy.ansible.com/download/ansible-posix-1.1.1.tar.gz to /root/.ansible/tmp/ansible-local-2115uc8975m5/tmp0h82jv5x
ansible.posix (1.1.1) was downloaded successfully
Writing requirements.yml file of downloaded collections to '/root/collections/requirements.yml'
检查目录并确认已经安装。
# cd collections/
[root@CentOS8-2 collections]# ls -ltr
合計 140
-rw-------. 1 root root 138226 12月 13 20:51 ansible-posix-1.1.1.tar.gz
-rw-r--r--. 1 root root 65 12月 13 20:51 requirements.yml
[root@CentOS8-2 collections]# cat requirements.yml
collections:
- name: ansible-posix-1.1.1.tar.gz
version: 1.1.1
为了安装离线缺失的模块,请执行以下命令。
$ansible-galaxy collection install -r requirements.yml
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Installing 'ansible.posix:1.1.1' to '/root/.ansible/collections/ansible_collections/ansible/posix'
ansible.posix (1.1.1) was installed successfully
缺少的模块将保存在以下目录中。
~/.ansible/collections/ansible_collections/
确认结果如下所示。
# ls -ltr ~/.ansible/collections/ansible_collections/ansible/posix/
.github/ README.md requirements.txt
.gitignore changelogs/ shippable.yml
CHANGELOG.rst docs/ test-requirements.txt
COPYING hacking/ tests/
FILES.json meta/
MANIFEST.json plugins/
再次执行2-4手册。
安装了缺失的模块后,再次执行playbook。
这次成功执行了playbook。
# ansible-playbook -i inventory playbook.v2.yml --check
PLAY [ap_servers] **************************************************************
TASK [Set authorized key took from file] ***************************************
changed: [192.168.11.19]
PLAY RECAP *********************************************************************
192.168.11.19 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0