当前位置: 首页 > news >正文

Ansible简单部署与使用

目录

  • 环境
  • 安装Ansible
    • apt install
    • markupsafe error
  • 配置Ansible
    • 创建个人目录
    • ansible.cfg
    • hosts
  • 测试Ansible
    • ping
    • 批量执行自定义命令

环境

  • Ubuntu 20.04

安装Ansible

apt install

sudo apt install ansible

markupsafe error

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

ubuntu@ubuntu:~$ ansible --version
Traceback (most recent call last):File "/usr/bin/ansible", line 62, in <module>import ansible.constants as CFile "/usr/lib/python3/dist-packages/ansible/constants.py", line 12, in <module>from jinja2 import TemplateFile "/usr/lib/python3/dist-packages/jinja2/__init__.py", line 33, in <module>from jinja2.environment import Environment, TemplateFile "/usr/lib/python3/dist-packages/jinja2/environment.py", line 15, in <module>from jinja2 import nodesFile "/usr/lib/python3/dist-packages/jinja2/nodes.py", line 23, in <module>from jinja2.utils import MarkupFile "/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的版本:

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版本可以解决:

python -m pip install markupsafe==2.0.1

ansible可成功运行:

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

配置Ansible

创建个人目录

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

mkdir -p /home/ubuntu/mydir/ansible

ansible.cfg

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

cp /etc/ansible/ansible.cfg /home/ubuntu/mydir/ansible

修改cfg文件:

[defaults]
inventory      = /home/ubuntu/mydir/ansible/hosts[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

hosts

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

cp /etc/ansible/hosts /home/ubuntu/mydir/ansible

修改hosts文件:

[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

测试受控主机的连通性:

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"
}

批量执行自定义命令

常用命令

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"

http://www.mrgr.cn/news/22460.html

相关文章:

  • C++11 的继续学习
  • 游泳馆押金管理+手牌管理+刷手牌 开通方法
  • 从状态管理到性能优化:全面解析 Android Compose
  • STM32(十二):DMA直接存储器存取
  • 【C++】STL学习——priority_queue(了解仿函数)
  • 防爆定位信标与防爆定位基站有什么区别?
  • 面板中的乐观更新(体验升级)
  • c++进阶——哈希表
  • java基础-IO(4)管道流 PipedOutputStream、PipedInputStream、PipedReader、PipedWriter
  • 逆元
  • C语言函数
  • Js实现继承的6种方式
  • 一文彻底搞懂:Java基本数据类型详解
  • C++11 atomic和内存序
  • 谈谈ES搜索引擎
  • 2409wtl,网浏包装
  • 前端页面加载由模糊到清晰的实现方案
  • 生信代码入门:从零开始掌握生物信息学编程技能
  • 使用 BentoML快速实现Llama-3推理服务
  • SpringMVC基础