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'
相关推荐
菜鸟xy..21 分钟前
winhex软件简单讲解,虚拟磁盘分区介绍
linux·运维·服务器
网硕互联的小客服25 分钟前
如何排查服务器内存泄漏问题
linux·运维·服务器·安全·ssh
驰驰的老爸28 分钟前
elk单机版安装
运维·jenkins
Evoxt 益沃斯34 分钟前
How to enable Qemu Guest Agent for Virtual Machines
linux·运维·服务器·qemu
CVer儿1 小时前
ubuntu挂载固态硬盘
linux·运维·ubuntu
明达技术1 小时前
MR30分布式IO携手PLC实现手工作业产线自动化升级
运维·分布式·自动化
hang11.1 小时前
Web服务器配置
运维·服务器
m0_674031431 小时前
Linux安装RabbitMQ
linux·运维·rabbitmq
m0_748251522 小时前
httpslocalhostindex 配置的nginx,一刷新就报404了
运维·nginx
亚林瓜子2 小时前
Minio安装(Docker Compose方式)
运维·docker·容器·minio·compose