前端analysis | 3w & 1h

ansible学习笔记一

2022-08-02

学习笔记

何为ansible

ansible 基于ssh协议,进行服务器管理的

核心概念

管理节点

管理远程机子的节点

主机目录

远程机子列表,可以分类标识

1
2
3
4
5
6
7
[web]
foo.example.com
bar.example.com

[db]
one.example.com
two.example.com

远程机子

需要操作的

Playbook

把命令文件yml化,减少错误

如何管理机子

命令行方式

-m 指定模块或者说使用什么命令 ; -a指定模块执行,需要的参数

1
2
 // 在web机组上,使用yum执行httpd包安装
ansible web -m yum -a "name=httpd state=present"

playbook方式

1
ansible-playbook xxx.yml

playbook 模块

ansible-doc [module]  or refer to the official documentation. 常用模块如下:

调试、测试类

  • ping: ping远程机子,ssh ok && python版本2.4+ ok,返回pong
  • debug : 简单打印一些消息, 类似linux echo

文件类模块

  • copy - 从本地复制到远程节点
  • template - 从本地复制到远程节点,并进行变量替换
  • file - 设置文件属性

Linux上的常用操作

  • user - 管理用户账号
  • yum - 包管理
  • service - 管理服务
  • firewalld - 管理防火墙中的服务和端口

执行shell命令

  • shell - 在节点上执行shell命令, 支持$HOME ,<,> |, &
  • command - 在远程节点上执行shell, 不支持$HOME这些

ansible怎么安装

Linux

1
2
# sudo yum install epel-release 
# sudo yum install ansible -y

ansible配置项怎么看

具体的可以在安装好ansible, /etc/ansible/ansible.cfg文件中查看, 常见的配置如下:

  • inventory 主机目录文件
  • library extra模块放置路径
  • remote_temp 远程主机临时文件位置
  • local_temp 管理节点上临时文件位置
  • accelerate_port 配置连接端口
  • accelerate_timeout 超时时间
  • accelerate_connect_timeout 连接超时时间

推荐学习

#get-started

#red-hat-ansible

#ansible-module

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏