Ansible简单部署与使用

目录

环境

  • Ubuntu 20.04

安装Ansible

apt install

bash 复制代码
sudo apt install ansible

markupsafe error

安装成功后,尝试运行ansible,部分环境下会有如下报错:

bash 复制代码
ubuntu@ubuntu:~$ ansible --version
Traceback (most recent call last):
  File "/usr/bin/ansible", line 62, in <module>
    import ansible.constants as C
  File "/usr/lib/python3/dist-packages/ansible/constants.py", line 12, in <module>
    from jinja2 import Template
  File "/usr/lib/python3/dist-packages/jinja2/__init__.py", line 33, in <module>
    from jinja2.environment import Environment, Template
  File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 15, in <module>
    from jinja2 import nodes
  File "/usr/lib/python3/dist-packages/jinja2/nodes.py", line 23, in <module>
    from jinja2.utils import Markup
  File "/usr/lib/python3/dist-packages/jinja2/utils.py", line 656, in <module>
    from markupsafe import Markup, escape, soft_unicode
ImportError: cannot import name 'soft_unicode' from 'markupsafe' (/home/ubuntu/.local/lib/python3.8/site-packages/markupsafe/__init__.py)

上述报错通常是markupsafe版本不兼容导致,查看markupsafe的版本:

bash 复制代码
ubuntu@ubuntu:~$ pip show markupsafe
Name: MarkupSafe
Version: 2.1.5
Summary: Safely add untrusted strings to HTML/XML markup.
Home-page: https://palletsprojects.com/p/markupsafe/
Author:
Author-email:
License: BSD-3-Clause
Location: /home/ubuntu/.local/lib/python3.8/site-packages
Requires:
Required-by: Werkzeug

回退到2.0.1版本可以解决:

bash 复制代码
python -m pip install markupsafe==2.0.1

ansible可成功运行:

bash 复制代码
ubuntu@ubuntu:~$ ansible --version
ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Nov 22 2023, 10:22:35) [GCC 9.4.0]

配置Ansible

创建个人目录

为了不影响系统配置,可以在个人的自定义目录下运行ansible,基于个人的配置文件:

bash 复制代码
mkdir -p /home/ubuntu/mydir/ansible

ansible.cfg

拷贝系统的cfg文件到个人目录下进行修改:

bash 复制代码
cp /etc/ansible/ansible.cfg /home/ubuntu/mydir/ansible

修改cfg文件:

powershell 复制代码
[defaults]
inventory      = /home/ubuntu/mydir/ansible/hosts

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

hosts

拷贝系统的hosts文件到个人目录下进行修改:

bash 复制代码
cp /etc/ansible/hosts /home/ubuntu/mydir/ansible

修改hosts文件:

powershell 复制代码
[my_test]
#10.5.3.202 ansible_ssh_port=22 ansible_ssh_user=test ansible_ssh_pass='123' ansible_su_pass='123'
10.5.3.202

[my_test:vars]
ansible_ssh_user=test
ansible_ssh_pass="123"

测试Ansible

ping

测试受控主机的连通性:

bash 复制代码
ubuntu@ubuntu:~/mydir/ansible$ ansible my_test -m ping
10.5.3.202 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

批量执行自定义命令

常用命令

bash 复制代码
ansible my_test -m shell -a "bash xxx.sh"
ansible my_test -m copy -a "src=/home/ubuntu/xxx.sh dest=/home/test/ mode=775"
ansible my_test -m file -a "path=/home/test/xxx.sh group=test owner=test"
相关推荐
一心0921 小时前
ubuntu 20.04.6 sudo 源码包在线升级到1.9.17p1
运维·ubuntu·sudo·漏洞升级
好好学习啊天天向上1 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
你想考研啊1 小时前
三、jenkins使用tomcat部署项目
运维·tomcat·jenkins
好奇的菜鸟2 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°2 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
代码老y2 小时前
Docker:容器化技术的基石与实践指南
运维·docker·容器
满昕欢喜2 小时前
SQL Server从入门到项目实践(超值版)读书笔记 20
数据库·sql·sqlserver
典学长编程2 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
wuk9983 小时前
基于MATLAB编制的锂离子电池伪二维模型
linux·windows·github
Hello.Reader4 小时前
Redis 延迟排查与优化全攻略
数据库·redis·缓存