Ansible 详解(二)Ad-Hoc

ansible 是一款轻量级自动化运维工具,由的 Python 语言开发,结合了多种自动化运维工具的特性,实现了批量系统配置,批量程序部署,批量命令执行等功能; ansible 是基于模块化实现批量操作的。

一、命令格式

ansible patterns -m module  -a arguments -i  inventory --become --ask-become-pass -vvvv

patterns

主机/组匹配格式

all 所有主机

单主机/组

  • host

  • group

多主机/组

host01;host02;......;host0x

group01;group02;......;group0x

host* :host开头的主机

host[00-99] :host开头编号00-99的主机

group*:group开头的组

group[00-99]:host开头编号00-99的组

group01:!group02:组01-组02

group01:&group02:组01&组02

group01:group02:&group03:!group04

module:使用模块

arguments:模块参数

inventory:主机目录

become:提权

ask-become-pass:提示输入提权密码

vvvv:打印日志详情

二、inventory

文件:hosts

localhost ansible_connect=localmytest ansible_host="xxx.xxx.xxx.xxx" ansible_user="silence" ansible_become_user="root" ansible_python_interpreter="/bin/env python2.6"

三、 ping

检查主机状态

ansible all -m ping -i hosts

四、command

在主机上执行系统命令

相对 shell,不会解析 HOME 等自定义环境变量,以及<,>,|,&等命令符。

ansible all -m  command -a ‘sleep 10’ -i hostsansible mytest -m command -a 'echo ${TEST}'  -i hosts

五、shell

在主机上执行 shell 命令

ansible all -m  shell -a ‘sleep 10’ -i hostsansible mytest -m shell -a 'echo ${TEST}'  -i hosts

六、raw

执行原始命令

ansible mytest -m  raw -a ‘echo ‘date’ >/tmp/current’ -i hosts

七、copy

复制文件

ansible mytest -m  raw -a ‘src= /etc/hosts dest=/tmp/hosts’ -i hosts

复制文件夹

ansible mytest -m  raw -a ‘src= /etc/profile.d/dest=/tmp/profile.d’ -i hosts

八、file

对文件或者文件夹进行操作

修改文件权限

ansible mytest -m file -a ‘dest=/tmp/hosts mode=777 owner=root  group=root’ -i hosts --become --ask-become-pass

创建文件夹

ansible mytest -m file -a ‘dest=/tmp/kk/01 mode=777 owner=silence  state=directory’ -i hosts

删除文件/文件夹

ansible mytest -m file -a ‘dest=/tmp/kk/ state=absent’ -i hosts

九、yum

注意:针对受控机器为 centos 系统,python 解释必须为系统默认安装的

安装到最新包

ansible mytest -m yum -a ‘name=tree state=latest’ -i hosts --become --ask-become-pass

卸载包

ansible mytest -m yum -a ‘name=tree state=absent’ -i hosts --become --ask-become-pass

安装安装包

ansible mytest -m yum -a ‘name=tree state=present’ -i hosts --become --ask-become-pass

安装指定版本包

ansible mytest -m yum -a ‘name=tree-1.5.3  state=present’ -i hosts --become --ask-become-pass

十 、user

对用户进行管理

openssl passwd -salt saltstring  password

创建用户,修改密码

ansible mytest -m user -a ‘name=kk  password=’ -i hosts --become --ask-become-pass

删除用户

ansible mytest -m yum -a ‘name=kk state=absent’ -i hosts --become --ask-become-pass

十一、git

对 git 代码进行管理

ansible mytest -m git -a ‘repo=http://github.com/imsilence/asynctask.git dest=/tmp/asynctask/version=HEAD’ -i hosts --become --ask-become-pass

十二、service

对服务进行管理

启动服务

ansible mytest -m service -a ‘name=nginx state=started’ -i hosts --become --ask-become-pass

停止服务

ansible mytest -m service -a ‘name=nginx state=stopped’ -i hosts --become --ask-become-pass

重启服务

ansible mytest -m service -a ‘name=nginx state=restarted’ -i hosts --become --ask-become-pass

十三、setup

查看受控机器信息

ansible mytest -m setup -i hosts

十四、异步

参数

-B:最大执行时间

-P:状态查询间隔

ansible mytest -m command -B 300 -P 0 -a ‘sleep 60’ -i hosts

通过 -vvvv 获取调试日志过滤 job

ansible mytest -m command -B 300 -P 0 -a ‘sleep 60’ -i hosts -vvvv|grep job

查询 job 的状态

ansible mytest -m async_status -a ‘jid=557725439653.20928 60’ -i hosts

十五、列表模块

https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

Ansible 详解(二)Ad-Hoc_第1张图片

未完待续......

公告通知

Python实战班、自动化运维班、区块链正在招生中

各位小伙伴们,欢迎试听和咨询:

Ansible 详解(二)Ad-Hoc_第2张图片

你可能感兴趣的:(Ansible 详解(二)Ad-Hoc)