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'
相关推荐
明航咨询_贾老师2 天前
RHCA Ansible高级自动化(DO374)认证信息整理
运维·自动化·ansible·rhca·红帽认证
有毒的教程6 天前
Ansible批量操作自动化完整教程:批量部署服务、配置同步、软件更新实战
运维·自动化·ansible
Hy行者勇哥11 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏13 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风23 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible