标签:维护 客户端 systemctl 启动 同步 ice file l命令 lis
**# 使用此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"
[root@baidu 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
标签:维护 客户端 systemctl 启动 同步 ice file l命令 lis
原文地址:https://blog.51cto.com/12328206/2428120