Ansible基本使用

目录

介绍

安装

inventory-主机清单

分组

子组

modules-模块

command

shell

script

file

copy

systemd

yum

get_url

yum_repository

user

mount

cron


介绍

ansible是基于python开发的自动化运维工具。架构相对比较简单,仅需通过ssh连接客户机执行任务即可,客户端无需额外安装代理。

ansible有2中使用方式:

ad-hoc:相当于使用单条的命令行

playbook:类似于脚本,将多条命令组合成一个任务

安装

linux系统标准的yum源可能没有ansible软件包,可以使用epel源

复制代码
yum install epel-release
yum install -y ansible

查看软件版本

复制代码
[root@m01 yum.repos.d]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

建议先修改2条默认配置,在/etc/ansible/ansible.cfg 去掉两行的注释

复制代码
#关闭主机验证
host_key_checking = False
#开启ansible日志
log_path = /var/log/ansible.log

inventory-主机清单

主机清单中明确了需要控制的主机,ansible执行任务,必须要先读取主机清单文件,默认读取/etc/ansible/hosts文件。

使用ansible时,主控端到被控端提前做好ssh免密登录,如何做免密请参考我的另一篇博客。

ssh免密登录-CSDN博客文章浏览阅读262次,点赞3次,收藏5次。ssh免密设置https://blog.csdn.net/wangweinan_5566/article/details/143287669

分组

每个分组中可以多个主机,如果主机没有做免密,也可以在文件中指定用户、密码,但是不推荐该方式。

复制代码
[web]    #分组名
172.16.1.7    #主机ip

[nfs]
172.16.1.31 ansible_user=root ansible_password=2 ansible_port=22

子组

复制代码
[data:children]    #子组名:children  children关键字
nfs    #分组名
backup
db

初步使用ansible

复制代码
[root@m01 ansible]# ansible data -m ping
172.16.1.31 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
172.16.1.41 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
172.16.1.51 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.1.51 port 22: No route to host",
    "unreachable": true
}

modules-模块

相当于linux中的各种命令,ansible有好几百个模块,需要自己慢慢熟悉,本次会介绍几个常用的模块。

ad-hoc使用格式:

ansible 主机清单 -m 模块 -a 模块中的选项

-i:主机清单

-m:模块

-a:模块选项

查看模块帮助:

ansible-doc -s 模块名

command

ansible默认模块,该模块相当于linux命令行,但是不支持特殊符号如:*、|等,只适合执行简单命令。

复制代码
[root@m01 ~]# ansible all -m command -a 'hostname'
172.16.1.7 | CHANGED | rc=0 >>
web01
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.41 | CHANGED | rc=0 >>
backup

shell

与command模块类似,但是该模块支持特殊符号。

复制代码
[root@m01 ~]# ansible all -m shell -a 'rm -rf  /tmp/*'
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you
need to use command because file is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
172.16.1.31 | CHANGED | rc=0 >>

172.16.1.41 | CHANGED | rc=0 >>

172.16.1.7 | CHANGED | rc=0 >>

同样的执行命令,在使用command模块时,不会生效。

script

ansible将本机上的脚本文件下发到被控端并运行。

先分发文件,在运行脚本运行完后会删除脚本文件。

复制代码
ansible all -m script -a '~/script-test.sh'

file

管理文件、管理目录、管理软链接

选项:

path:文件

src:源文件

state:状态

state=directory 创建目录

state=file(默认) 更新文件,文件不存在则创建

state=link 创建连接

state=touch 创建文件

state=absent 删除(递归删除)

mode:权限

owner:所有者

group:所有组

创建文件:

复制代码
ansible all -m file -a 'path=/opt/wang.txt state=touch'

创建目录:

复制代码
ansible all -m file -a 'path=/opt/a/b/c/d state=directory'

创建软连接:

复制代码
ansible all -m file -a 'path=/opt/hosts src=/etc/hosts state=link'

删除文件:

复制代码
ansible all -m file -a 'path=/opt/wang state=absent'

copy

scp方式发送文件到被管理端

选项:

src:源文件

dest:目标文件

backup:目标文件存在,则备份

owner:所有者

group:所有组

传输文件:

复制代码
ansible all -m copy -a 'src=/etc/hosts dest=/etc/hosts backup=yes'

systemd

相当于systemctl命令,管理服务

选项:

name:服务名换

enabled:开机启动

yes

no

state:服务状态

started

stopped

reloaded

restarted

daemon-reload:加载对应的服务管理配置文件

yes

no

启动crond服务并开机自启:

复制代码
ansible all -m systemd -a 'name=crond state=started enabled=yes'

yum

包含yum/apt命令

选项:

name:软件包名,指定过个","分割

state:

installed/present 安装

removed/absent 删除

lastest 安装或更新

update_cache:缓存

yes

no

安装软件:

复制代码
ansible all -m yum -a 'name=htop,tree,lrzsz state=installed'

get_url

下载文件,相当于wget

选项:

url:下载地址

dest:下载目录

复制代码
ansible all -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/7/x86_64/zabbix-agent2-6.0.13-release1.el7.x86_64.rpm dest=/tmp'

yum_repository

下发yum源

提前写好,copy下发更直接

选项:

name:yum源中的名字 \[\]

description:yum源中的注释 name

baseurl:yum源的下载地址 baseurl

enabled:yum源是否启用

gpgcheck:yum源是否验证

file:yum源文件的名字,自动加repo

下发nginx yum源文件:

复制代码
ansible web -m yum_repository -a 'name=nginx description=nginx_repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ enabled=yes gpgcheck=no file=nginx'

user

管理用户

选项:

name:用户名

uid:uid

group:用户组

shell:解释器

create_home:是否创建家目录

state:

present 添加

absent 删除

创建虚拟用户:

复制代码
ansible all -m user -a 'name=www-ans uid=2000 shell=/sbin/nologin create_home=no state=present'

创建用户并设置密码:

复制代码
ansible all -m user -a "name=wem password={{'1' | password_hash('sha512','wang')}} state=present"

password选项只能接收加密过后的密码

{{'1' | password_hash('sha512','wang')}}

1:密码

password_hash:加密插件

sha512:加密算法

wang:随机字符串

mount

mount挂载或修改/etc/fstab实现永久挂载

选项:

fstype:文件系统 xfs ext4 nfs

src:源地址(nfs)

path:挂载点

state:

absent 卸载并修改fstab

umounted 卸载不修改fstab

present 仅修改fstab,不挂在

mounted 挂载并修改fstab

remounted 重新挂载

挂载nfs:

复制代码
ansible web -m mount -a 'src=172.16.1.31:/data/ path=/data/ fstype=nfs state=mounted'

cron

管理定时任务,删除任务时只能删除通过ansible添加的定时任务。

选项:

name:名字(即注释)

minute:分钟

hour:小时

day:日期

month:月

week:周

job:指定的命令或脚本

state:

present 添加

absent 删除

复制代码
ansible all -m cron -a 'name="sync time test" minute=*/2 job="/sbin/ntpdate ntp1.aliyun.com" state=present'
相关推荐
XIAOHEZIcode15 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉4 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦4 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw