ansible续集

一 . file

group # 属组
mode  # 权限
owner # 属主
path  # 路径
    state =link
    state =hard
state
    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.txt ansible 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 # 默认
    remove

ansible 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
    reloaded
user  # 启动的用户
ansible web -m service -a "name=redis state=started" # 启动redis
ansible web -m service -a "name=redis state=stopped" # 关闭redis
ansible web -m service -a "name=redis enabled=yes" # 设置开机自启动

五 . cron

minute ->分钟
hour ->小时
day ->天
month ->月
weekday ->周
job 任务
disabled ->禁用crontab,表现形式在任务之前加'#' 相当于注释
name 名字,描述信息
user 用户

添加时名字必须不同,不加名称为None
ansible web -m cron -a "minute=12 name=story job='touch /tmp/tianlong.txt'" # 每当分钟为12时候执行job
ansible 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/shadow
id 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 #设置用户登录后的shell
useradd -s /sbin/nologin attila6
-u, --uid UID #设置用户的id
useradd -u 2000 attila7
设置了用户的id以后,在设置用户则从最大的id开始往后数

===============ansible==================
group 属组
groups 附加组
home 设置家目录
name 用户名
remove 删除用户并删除用户的家目录
shell 用户登录后的shell
system 系统用户
uid 用户的id

ansible 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

# 正常操作的group
groupadd 
-g 设置id
-r 系统组
groupadd -g 3000 attila66  # id为3000 组名为attila66
groupadd -r attila66

查看
tail /etc/group

==================ansible====================

gid 组id
system 系统组
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'

 

 

原文地址:https://www.cnblogs.com/attila/p/10821772.html