提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、linux安装Ansible

1. 确保已安装 python3 和 pip

sudo dnf install python3 python3-pip

2. 为当前用户安装(推荐,不影响系统)

pip3 install --user ansible
# 或者全局安装
sudo pip3 install ansible

报错,提示需更新pip。
在这里插入图片描述

3. 更新pip

python3 -m pip install --upgrade pip

4. 将用户目录下的二进制文件路径加入 PATH

如果用了 --user 安装,需将 ~/.local/bin 加入环境变量。

echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

5. 验证安装

ansible --version

[root@localhost ~]# ansible --version
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. 
Current version: 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be
 removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
/root/.local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.exceptions import InvalidSignature
ansible [core 2.11.12] 
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.local/lib/python3.6/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/.local/bin/ansible
  python version = 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.0.3
  libyaml = True

核心安装目录 (根目录)

~/.local/lib/python*/site-packages/

核心可执行文件路径

~/.local/bin/

二、尝试使用ansible连接mysql容器

1.查看mysql容器

#确认mysql运行正常
docker ps | grep mysql
#查看容器IP
docker inspect mysql | grep -i ipaddress
#查看容器端口
docker inspect mysql | grep -A 10 "Ports"

2.安装community.mysql 集合

ansible-galaxy collection install community.mysql
提前安装PyMySQL
sudo pip3 install PyMySQL

3.创建命令集合playbook

在/etc/ansible目录下vi mysqltest.yaml

---
- name: Test MySQL connection with a simple query
  hosts: localhost
  tasks:
    - name: Attempt to connect to MySQL
      community.mysql.mysql_query: # 需要安装 `community.mysql` 集合
        login_host: 172.17.0.2
        login_port: 3306
        login_user: root # 替换为你的用户名
        login_password: 123456 # 替换为你的密码
        query: SELECT 1
      ignore_errors: yes # 忽略错误,只为了测试连通性

4.执行ansible-playbook测试

[root@localhost ansible]# ansible-playbook mysqltest.yaml 
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. 
Current version: 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]. This feature will be
 removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
/root/.local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
  from cryptography.exceptions import InvalidSignature
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'

PLAY [Test MySQL connection with a simple query] ********************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]

TASK [Attempt to connect to MySQL] **********************************************************************************
ok: [localhost]

PLAY RECAP **********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


更多推荐