一 . file
group # 属组mode # 权限owner # 属主path # 路径 state =link state =hardstate directory 目录 file touch 空文件 absent 删除 link 软连接 hard 硬链接 ansible web -m file -a "path=/tianlong state=directory owner=xiaofeng" # 创建目录,并制定属主,前提你有xiaofeng这个用户ansible web -m file -a "path=/tmp/yitian.txt state=touch mode=777" # 创建文件,并指定权限ansible web -m file -a "path=/opt/xxx.txt src=/opt/www.txt state=link" # 创建软链接,前提xxx.txt不能存在,www.txt是存在的,xxx.txt软连接到www.txtansible web -m file -a "path=/opt/xxx.txt state=absent" # 删除软连接ansible web -m file -a "path=/tianlong state=absent" # 删除文件夹软连接 快捷方式 ln -s 源文件修改软连接修改 源文件删除软连接失效 可以跨分区 硬链接 硬盘的位置 ln 源文件修改硬链接修改 源文件删除硬链接不变 不可以跨分区复制 开辟新空间 cp 源文件修改cp的不变 源文件删除不变 可以跨分区
二 . fetch
dest # 目标地址,管控机地址src # 源地址 写的是被控机地址(远程地址)ansible web -m fetch -a "src=/opt/attila/shuai.txt dest=/opt" # 拉取远程主机的文件,并以主机ip地址或者主机名为目录,并且保留了原来的目录结构
三 . yum
yum跟rpm有什么关系,有什么区别 rpm redhat package manager yum 可以解决依赖关系disablerepo # 禁用某个源enablerepo # 启用某个源yum grouplist # 查包组信息name # 包名state install # 默认 removeansible web -m yum -a "name=python2-pip" # 安装单个软件包ansible web -m yum -a "name=python2-pip,redis" # 安装多个包ansible web -m yum -a "name='@Development Tools'" # 安装包组ansible web -m yum -a "name=nginx state=absent" # 卸载
四 . service
由于ansible发布的时候还没有centos7,所以用的不是systemctl用的是centos6的service
enabled # 开机启动name # 服务名称state started stopped restarted reloadeduser # 启动的用户ansible web -m service -a "name=redis state=started" # 启动redisansible web -m service -a "name=redis state=stopped" # 关闭redisansible web -m service -a "name=redis enabled=yes" # 设置开机自启动
五 . cron
minute ->分钟hour ->小时day ->天month ->月weekday ->周job 任务disabled ->禁用crontab,表现形式在任务之前加'#' 相当于注释name 名字,描述信息user 用户添加时名字必须不同,不加名称为Noneansible web -m cron -a "minute=12 name=story job='touch /tmp/tianlong.txt'" # 每当分钟为12时候执行jobansible web -m cron -a "name=story state=absent" # 删除ansible web -m cron -a "minute=12 name=story2 job='touch /tmp/tianlong.txt' disabled=yes" # 注释ansible web -m cron -a "name=None state=absent" # 删除名称为None的计划任务
六 . user
# 不用ansible操作用户是的方法tail /etc/passwd # 查看有哪些用户tail /etc/shadowid attila 查看attila用户信息useradd -d 设置用户家目录useradd -d /opt/attila2 attila2 把用户attila2的家目录设置成/opt/attila2-g 设置用户的属组useradd -g attila2 attila3 -G, --groups 附加组useradd -G attila2,root attila4 # attila2,root都是attila4的附加组-r, --system 系统账号useradd -r attila5 # 系统账号没有家目录-s, --shell #设置用户登录后的shelluseradd -s /sbin/nologin attila6-u, --uid UID #设置用户的iduseradd -u 2000 attila7设置了用户的id以后,在设置用户则从最大的id开始往后数===============ansible==================group 属组groups 附加组home 设置家目录name 用户名remove 删除用户并删除用户的家目录shell 用户登录后的shellsystem 系统用户uid 用户的idansible web -m user -a "name=attila2 shell=/sbin/nologin home=/opt/attila2 uid=3000 groups=root" # 创建用户,并指定用户的shell,家目录,uid,以及附加组ansible web -m user -a "name=attila2 shell=/sbin/nologin home=/opt/attila2"ansible web -m user -a "name=attila3 system=yes" # 创建系统用户ansible web -m user -a "name=attila3 state=absent" #删除用户,单不删除家目录ansible web -m user -a "name=attila3 state=absent remove=yes" # 删除用户并删除用户的家目录
七 . group
# 正常操作的groupgroupadd -g 设置id-r 系统组groupadd -g 3000 attila66 # id为3000 组名为attila66groupadd -r attila66查看tail /etc/group==================ansible====================gid 组idsystem 系统组name 名称ansible web -m group -a "name=attila66 system=yes gid=5000" # 创建系统组ansible web -m group -a "name=attila88" #创建普通的组ansible web -m group -a "name=attila88 state=absent" # 删除组
八 . replace
# 给nginx配置文件的反向代理ip地址前边加上#号(加上注释,可用于机器下线)ansible 192.168.111.128 -m replace -a 'path=/opt/nginx/nginx.conf regexp=^(192.168.111.129) replace=#\1'# 去掉#号 解开注释ansible 192.168.111.128 -m replace -a 'path=/opt/nginx/nginx.conf regexp=#(192.168.111.129) replace=\1'