ansible基础命令

文章目录

  • [一、Ansible 概述和运行机制](#一、Ansible 概述和运行机制)
    • [1.1 Ansible 特点:](#1.1 Ansible 特点:)
    • [1.2 Ansible 工作机制](#1.2 Ansible 工作机制)
    • [1.3 Ansible 核心组成:](#1.3 Ansible 核心组成:)
    • [1.4 Ansible 角色 (Role)](#1.4 Ansible 角色 (Role))
  • [二、Ansible 环境安装部署](#二、Ansible 环境安装部署)
  • [三、Ansible 基础命令及模块操作](#三、Ansible 基础命令及模块操作)
    • [3.1 command 模块](#3.1 command 模块)
    • [3.2 shell 模块](#3.2 shell 模块)
    • [3.3 cron 模块(管理任务计划)](#3.3 cron 模块(管理任务计划))
    • [3.4 user 模块(用户管理)](#3.4 user 模块(用户管理))
    • [3.5 group 模块(用户组管理)](#3.5 group 模块(用户组管理))
    • [3.6 copy 模块(复制文件或内容)](#3.6 copy 模块(复制文件或内容))
    • [3.7 file 模块(文件管理)](#3.7 file 模块(文件管理))
    • [3.8 hostname 模块(修改主机名)](#3.8 hostname 模块(修改主机名))
    • [3.9 ping 模块](#3.9 ping 模块)
    • [3.10 script 模块](#3.10 script 模块)

--

一、Ansible 概述和运行机制

Ansible 是一款面向类 Unix 系统的自由开源配置和自动化工具,由 Python 编写。与 SaltStack、

Puppet、Chef 相似,但具有以下优势:

无需在被管理节点安装客户端,轻量级。

通过 SSH 协议与节点通信。

使用 YAML 和 Jinja2 模板语言进行配置和自动化任务编排。

1.1 Ansible 特点:

  1. 部署简单,管理端安装即可,被控端无需操作;
  2. 默认使用 SSH 协议管理设备;
  3. 集中化管理,支持主从模式;
  4. 配置简洁、功能强大、扩展性高;
  5. 支持 API 和自定义模块,可通过 Python 扩展;
  6. Playbook 可实现复杂任务配置和状态管理;
  7. 对云计算和大数据平台支持良好。

1.2 Ansible 工作机制

Ansible 通过 SSH 将模块推送到被管理节点执行,执行完后自动删除。可结合 SVN 等工具管理自定义模块及任务编排。

1.3 Ansible 核心组成:

  1. Ansible:核心引擎
  2. Modules:内置核心模块和自定义模块
  3. Plugins:补充模块功能,如连接插件、邮件插件等
  4. Playbooks:剧本,定义多任务操作
  5. Inventory:主机清单

1.4 Ansible 角色 (Role)

随着数据中心环境复杂化,Playbook 会变得庞大且难以维护。角色提供了对复杂任务的模块化管理:

  • 将任务组织为独立、可复用的剧本和文件
  • 提供从外部加载任务、处理程序、变量的机制
  • 可关联静态文件和模板
  • 满足通用需求,可重复使用
  • 严格的目录结构要求

二、Ansible 环境安装部署

1、管理端安装 Ansible:

bash 复制代码
yum install -y epel-release # 安装 EPEL 源
yum install -y ansible

(主机清单目录结构:/etc/ansible/hosts)

2、配置主机清单:

bash 复制代码
[webservers] #配置组名
192.168.10.14 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
[dbservers]
192.168.10.15

3、配置免密 SSH 登录:

bash 复制代码
ssh-keygen -t rsa # 生成密钥
sshpass -p '123456' ssh-copy-id root@192.168.10.14
sshpass -p '123456' ssh-copy-id root@192.168.10.15

三、Ansible 基础命令及模块操作

3.1 command 模块

bash 复制代码
ansible 192.168.10.14 -m command -a 'date' #指定 ip 执行 date
ansible webservers -m command -a 'date' #指定组执行 date
ansible webservers -m command -a 'date' #指定组执行 date
ansible dbservers -m command -a 'date'
ansible all -m command -a 'date' #all 代表所有 hosts 主机
ansible all -a 'ls /' #如省略 -m 模块,则默认运行 command模块

3.2 shell 模块

bash 复制代码
ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") |
cut -d " " -f2'
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

3.3 cron 模块(管理任务计划)

bash 复制代码
ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld"
name="test crontab"'
ansible webservers -a 'crontab -l'
ansible webservers -m cron -a 'name="test crontab" state=absent' #移除
计划任务,假如该计划任务没有取名字,name=None即可

3.4 user 模块(用户管理)

bash 复制代码
//常用的参数:
name:用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:用户基本组
shell:默认使用的shell
move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动
password:用户的密码,建议使用加密后的字符串
comment:用户的注释信息
remove=yes|no:当state=absent时,是否删除用户的家目录
bash 复制代码
ansible dbservers -m user -a 'name="test01"' #创建用户test01
ansible dbservers -m command -a 'tail /etc/passwd'
ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01

3.5 group 模块(用户组管理)

bash 复制代码
ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #创建mysql组
ansible dbservers -a 'tail /etc/group'
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #将
test01用户添加到mysql组中
ansible dbservers -a 'tail /etc/passwd'
ansible dbservers -a 'id test01

3.6 copy 模块(复制文件或内容)

bash 复制代码
//用于复制指定主机文件到远程主机的
ansible-doc -s copy
//常用的参数:
dest:指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存
在会覆盖原有的内容
src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目
录
mode:指出复制时,目标文件的权限
owner:指出复制时,目标文件的属主
group:指出复制时,目标文件的属组
content:指出复制到目标主机上的内容,不能与src一起使用
bash 复制代码
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root
mode=640'
ansible dbservers -a 'ls -l /opt'
ansible dbservers -a 'cat /opt/fstab.bak'
ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #将
helloworld写入/opt/hello.txt文件中
ansible dbservers -a 'cat /opt/hello.txt'

3.7 file 模块(文件管理)

bash 复制代码
//设置文件属性
ansible-doc -s file
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644
path=/opt/fstab.bak' #修改文件的属主属组权限等
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak
state=link' #设置/opt/fstab.link为/opt/fstab.bak的链接文件
ansible dbservers -m file -a "path=/opt/abc.txt state=touch" #创建一个
文件
ansible dbservers -m file -a "path=/opt/abc.txt state=absent" #删除一个
文件

3.8 hostname 模块(修改主机名)

bash 复制代码
//用于管理远程主机上的主机名
ansible dbservers -m hostname -a "name=mysql01"

3.9 ping 模块

bash 复制代码
检测主机连通性
ansible all -m ping

3.10 script 模块

执行本地脚本

bash 复制代码
//实现远程批量运行本地的 shell 脚本
ansible-doc -s script
vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt
chmod +x test.sh
ansible webservers -m script -a 'test.sh'
ansible webservers -a 'cat /opt/script.txt'
相关推荐
叫致寒吧3 小时前
自动化运维工具 Ansible
运维·自动化·ansible
m0_488777654 小时前
Ansible基础概念及相关命令
ansible·模块·自动化运维工具
2501_939909054 小时前
自动化运维工具 Ansible 集中化管理服务器
运维·自动化·ansible
hanyi_qwe4 小时前
自动化运维工具 Ansible 集中化管理服务器
运维·自动化·ansible
LucidX4 小时前
Ansible ——核心模块
ansible
叫致寒吧5 小时前
Ansible-Playbook 剧本编写
ansible
The star"'20 小时前
04-管理变量和事实
运维·云计算·ansible
tzhou6445221 小时前
自动化运维利器Ansible
运维·自动化·ansible
可爱又迷人的反派角色“yang”1 天前
ansible的概念及基本操作(一)
运维·ansible