ansible 常用命令

**# 使用此user的su执行操作,默认为root,已废弃,使用become替代 -b --become 使用become的方式升级权限
因为我们使用的是普通用户来操作**

-i 目标主机列表 -m 指定模块名称
**ansible ping 模块**
ansible -i /etc/ansible/ssh/hosts all -m ping -b

**ansible copy 模块**
-a 模块的参数或者命令 force=yes:用来强制取消软件安装过程中的交互确认提示 第一次使用时候
ansible -i /etc/ansible/ssh/hosts all -m copy -a "src=/root/test.sh dest=/tmp/test.sh mode=755
owner=root group=root force=yes" -b

content:表示文件内容把helloworld加入test.txt backup=yes 备份
ansible -i /etc/ansible/ssh/hosts huawei -m copy -a ‘content="hello world" dest=/tmp/test.txt
backup=yes mode=755 owner=root‘ -b

**ansible command 模块 主要执行linux基础命令**
ansible -i /etc/ansible/ssh/hosts all -m command -a "date" -b
ansible -i /etc/ansible/ssh/hosts huawei -m command -a "systemctl start nginx" -b

**ansible yum 模块** 主要用于软件安装 升级 卸载absent
name: 安装最新版本的apache
yum: name=httpd state=latest
name: 移除apache
yum: name=httpd state=absent
name: 安装一个特殊版本的apache
yum: name=httpd-2.2.29-1.4.amzn1 state=present
name: 升级所有的软件包
yum: name=* state=latest
name: 从一个远程yum仓库安装nginx
yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state=present
name: 从本地仓库安装nginx
yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present
name: 安装整个Development tools相关的软件包
yum: name="@Development tools" state=present
ansible -i /etc/ansible/ssh/hosts huawei -m yum -a "name=sysstat,screen state=installed" -b

**ansible file模块** 文件、创建、删除、修改、权限、属性、维护和管理
修改文件属性
ansible -i /etc/ansible/ssh/hosts huawei -m file -a "path=/tmp/test.txt owner=nginx group=nginx
mode=0644" -b
生成链接文件
ansible -i /etc/ansible/ssh/hosts huawei -m file -a "src=/root/test.sh dest=/root/testlink.sh
owner=root group=root state=link force=yes" -b
创建空文件
ansible -i /etc/ansible/ssh/hosts huawei -m file -a "path=/root/abc.sh state=touch mode=0644" -b
创建空目录
ansible -i /etc/ansible/ssh/hosts huawei -m file -a "path=/root/abc state=directory mode=755" -b
删除目录或文件,强制执行
ansible -i /etc/ansible/ssh/hosts huawei -m file -a "path=/root/abc state=absent force=yes" -b

**ansible user group模块** 生成用户、删除用户
生成用户
ansible -i /etc/ansible/ssh/hosts huawei -m user -a "name=test password=1DhUWqz2JZqc home=/home
uid=999 comment=‘this is a ansible test user‘ shell=/bin/sh" -b
删除用户remove是否移除家目录
ansible -i /etc/ansible/ssh/hosts huawei -m user -a "name=test state=absent remove=yes force=yes" -b
生成组
ansible -i /etc/ansible/ssh/hosts huawei -m group -a ‘name=g1 gid=666 state=present system=yes‘ -b
删除组
ansible -i /etc/ansible/ssh/hosts huawei -m group -a ‘name=g1 state=absent‘ -b

**ansible cron 模块** present(添加)、absent(移除) job:任何计划执行的命令,state要等于present
同步时间每天凌晨0点0分
ansible -i /etc/ansible/ssh/hosts huawei -m cron -a "minute=0 hour=0 day=* weekday=* name=‘server
status‘ job=‘/usr/sbin/ntpdat 139.224.227.121‘" -b
备份
ansible -i /etc/ansible/ssh/hosts huawei -m cron -a "minute=0 hour=0 day=* month=* weekday=*
name=‘Ntpdate server for sync time‘ backup=yes job=‘ntpdate www.lcl.com‘"
删除
ansible -i /etc/ansible/ssh/hosts huawei -m cron -a "name=‘server status‘ state=absent" -b

**ansible 修改主机名字:hostname**
ansible -i /etc/ansible/ssh/hosts huawei -m hostname -a "name=ansible-test245" -b

**Ansible synchronize模块主要用于目录、文件的同步,主要基于rsync命令工具同步目录和文件**
(1)常用的src为源目录,dest为目标目录,示例为将远程主机的/tmp/目录同步为本地Ansible服务器的/tmp/目录
需要些安装rsync 软件
ansible -i /etc/ansible/ssh/hosts huawei -m synchronize -a "src=/tmp/ dest=/tmp/" -b
(2)compress=yes表示开启压缩,delete表示数据一致,rsync_opts表示同步参数,–exclude表示排除文件
ansible -i /etc/ansible/ssh/hosts huawei -m synchronize -a "src=/tmp dest=/mnt compress=yes delete=yes
archive=yes rsync_opts=--exclude=*.txt" #这句表示把本地的/tmp目录以及里面除了.txt结尾的文件同步到huawei
的/mnt目录里面,并且同步/tmp目录以及文件的属性,还要删除本地没有但远程主机有的文件

**Ansible shell模块主要用于远程客户端上执行各种shell命令或者运行脚本**
(1)远程执行shell脚本,并把结果追加至服务器/tmp/var.log文件,这种用shell执行而不用command,就是因为command
不支持变量、重定向、管道符等操作
ansible -i /etc/ansible/ssh/hosts huawei -m shell -a "/bin/sh /mnt/test.sh >> /tmp/var.log" -b
ansible -i /etc/ansible/ssh/hosts huawei -m command -a "cat /tmp/var.log" -b
(2)执行之前切换目录,屏蔽警告信息,创建目录
ansible -i /etc/ansible/ssh/hosts huawei -m shell -a "mkdir -p `date +%F` chdir=/tmp/ warn=no" -b
##这里是为了创建一个今天日期的目录,所以date +%F不是引号而是和shell脚本中使用命令时使用的反引号
(3)用shell可以使用管道符,比如查看远程客户端nginx服务是否启动,用默认的command模块就会报错,这里也显示shell
模块和command模块的区别
ansible -i /etc/ansible/ssh/hosts huawei -m shell -a "ps -ef | grep http"
[[email protected] tmp]# ansible -i /etc/ansible/ssh/hosts huawei -m command -a "ps -ef | grep nginx"
1.1.1.1 | FAILED | rc=1 >>
error: garbage option
Usage:
ps [options]
Try ‘ps --help <simple|list|output|threads|misc|all>‘
or ‘ps --help <s|l|o|t|m|a>‘
for additional help text.
For more details see ps(1).non-zero return code

**Ansible常用模块-script模块**
script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本
地,不需要手动拷贝到远程主机后再执行。
ansible -i /etc/ansible/ssh/hosts all -m script -a "test.sh" -b #需要在脚本所在目录里面执行

**Ansible service模块主要用于远程客户端各种服务管理,包括启动、停止、重启、重新加载等**
enabled:是否开机启动服务
name:服务名称
runlevel:服务启动级别
arguments:服务命令行参数传递
state:服务操作状态,状态包括started、stopped、restarted、reloaded
#停止nginx
ansible -i /etc/ansible/ssh/hosts huawei -m service -a "name=nginx state=stopped" -b
#将服务设置成开机自启动
ansible -i /etc/ansible/ssh/hosts huawei -m service -a "name=nginx enabled=yes" -b

原文地址:https://blog.51cto.com/12328206/2428120

时间: 2024-07-30 14:43:50

ansible 常用命令的相关文章

ansible常用命令及模块的使用方法

ansible是指令核心部分,其主要用于执行ad-hoc(点对点)命令,即单条命令.默认后面需要跟主机和选项部分,默认不指定模块时,使用的是command模块. 2. 默认使用的模块是可以在ansible.cfg 中进行修改的. /etc/ansible/ansible.cfg ansible常用命令解析: 查看当前 ansible 都支持哪些模块: ansible-doc -l 查看某一模块可以使用的参数:ansible-doc -s copy (eg:copy模块) ansible用法: a

ansible常用命令

ad-hoc常用命令是用来解决一些常用的简单的配置命令,而且这些命令的执行速度很快.adhoc可以不用写playbooks,使用方便简单. man ansible ansible <pattern> [-f forks] [-m module] [-a args] ARGUMENTS pattern 组名,或者主机名,匹配hosts文件. OPTIONS -v --verbose 详细模式,如果命令执行成功,输出详细的结果, -i PATH --inventory=PATH 指定host文件的

ansible常用命令大全

ansible '*' -m command -a 'uptime' '*':自己定义的主机       -m command:命令 # 检查主机连接 ansible '*' -m ping # 执行远程命令 ansible '*' -m command -a 'uptime' # 执行主控端脚本 ansible '*' -m script -a '/root/test.sh' # 执行远程主机的脚本 ansible '*' -m shell -a 'ps aux|grep zabbix' #

Ansible常用模块介绍及使用

ansible常用命令解析: 查看当前 ansible 都支持哪些模块: ansible-doc -l 查看某一模块可以使用的参数:ansible-doc -s copy (eg:copy模块) ansible用法: ansible 主机名 -m 模块名 -a 模块参数 -m:模块的名字,如果执行单一的命令不用加-m,默认走command -a: 模块参数,如果模块是command,-a 后面跟的就是命令参数 -i : 指定hosts文件位置      默认:/etc/ansible/hosts

Ansible Ad-Hoc 常用命令

Ansible Ad-Hoc常用命令 Ansible可以通过命令行形式使用它的模块,Ansible自带了很多模块,可以直接使用这些模块.目前Ansible已经自带了259个模块,可以通过ansible-doc -l 显示所有自带的模块,也可以通过ansible-doc -s 模块名 查看模块的介绍及使用示例. Ansible命令行的基本格式如下: ansible <host-pattern> [-m modult_name] [-a agrs] 基本语法说明: <host-pattern

详解Ansible服务常用命令模块

ansible可以使用命令行方式进行自动化管理,基本语法如下: ansible 主机名 -m 模块名称 -a 模块特有参数 ansible的命令行管理工具都是由一系列模块.参数所支持的,可以在命令后面加上-h或--help获取帮助.如使用ansible-doc -h或者ansible-doc --help查看其帮助信息ansible-doc是用来查看模块帮助信息的工具,最主要的选项 -l用来列出可使用的模块, -s用来列出某个模块的描述信息和使用示例. 以下是我列出yum模块的描述信息和操作动作

ansible常用模块介绍

ansible查看模块用法 例子 查看shell 模块使用方法 [[email protected] opt]# ansible-doc -s shell 注: -i 指定配置文件  tomcatserver自己定义的工作组  -m 指定模块 file模块 file模块包含如下选项: force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下:另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no group:定义文件/目录的属组 mo

ansible常用模块详解

ansible常用模块详解: ansible <host-pattern> [-m module_name] [-a args] [options] #ansible命令格式  指定主机组或ip地址  指定调用模块   传递给模块的参数   ansible-doc -l #列出可用模块 ansible-doc -s model_name #查看指定模块详细用法 command:ansible默认模块,对指定主机执行命令,不能理解特殊字符 例:ansible web -a 'date' #对we

ansible 常用模块

执行前设定 它的Config文件呢,默认在/etc/ansible/ansible.cfg 第一步,就是我们的ansible的机器,需要链接上被它控制的机器.因为ansible的ssh是默认有个检查key的设置,我们第一次使用它,肯定对面机器没有Public key啊,所以我们要关闭配置文件内的private key的检查: host_key_checking = False 生成ssh-key 生成ssh-key这部分,就不再多说了. 创建你的hosts 设置控制的主机清单在/etc/ansi