ansible模块总结2

一.file

  文件或者文件夹的操作

参数

group # 属组
mode #权限
owner #属主
path #路径
    state =link
    state =hard
state
    directory 目录
    file
    touch 空文件
    absent 删除
    link 软连接
    hard 硬链接

示例

ansible web -m file -a "path=/tom5 state=directory owner=tom" #创建目录,并制定属主
ansible web -m file -a "path=/tmp/bob.txt state=touch mode=777" #创建文件,并指定权限
ansible web -m file -a "path=/tmp/cron src=/var/log/cron state=link" #创建软链接,链接的是自己的文件
ansible web -m file -a "path=/tmp/cron state=absent" # 删除软连接
ansible web -m file -a "path=/tom5 state=absent" #删除文件夹

软连接与硬连接对比

软连接  快捷方式  ln -s   源文件修改软连接修改  源文件删除软连接失效  可以跨分区 
硬链接  硬盘的位置 ln     源文件修改硬链接修改  源文件删除硬链接不变 不可以跨分区
复制    开辟新空间 cp     源文件修改cp的不变   源文件删除不变 可以跨分区

二.fetch

  拉取远程主机的文件,并以主机ip地址或者主机名为目录,并且保留了原来的目录结构

参数

dest 目标地址
src 源地址

示例

ansible web -m fetch -a "src=/var/log/cron dest=/tmp" 

三.yum

  安装linux的软件包

1.yum跟rpm有什么关系,有什么区别

rpm redhat package manager
yum 可以解决依赖关系

2.yum源怎么配置

扫描二维码关注公众号,回复: 6142173 查看本文章
[epel] #名称
name=Extra Packages for Enterprise Linux 7 - $basearch #全名或者描述信息
baseurl=http://mirrors.aliyun.com/epel/7/$basearch # 源url地址
failovermethod=priority
enabled=1 #是否启用,1启用,0不启用
gpgcheck=0 #是否检验key文件,0不校验 1校验
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

3.yum怎么安装包组

yum grouplist #查包组信息

参数

disablerepo #禁用某个源
enablerepo #启用某个源
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" #卸载

四.pip

  安装python的包

参数

chdir #切换目录
name #包名
requirements #导出的文件
virtualenv #虚拟环境

基本使用

pip freeze > a.txt #将本地环境导出
pip install -r a.txt  #安装所有的包
pip list #查看所有的包
pip uninstall flask #卸载
# 下载包
Python setup.py build 
python setup.py install

示例

  ansible中的pip,被控机上应安装有pip才行

ansible  mygroup -m pip -a "name=django==1.11.14"  # 给被控机安装django

五.service

关于service命令

启动
  systemctl start redis    #centos7
  service redis start     #centos6
开机自启动
  systemctl enable redis    #centos7
  chkconfig redis on        #centos6
init
  0
关机 1单用户 3命令行 5图形界面 6重启
查进程的两种方式 1. 
ps -ef|grep redis
2. ss -tunlp     
-t tcp     -u udp     -n 以端口形式显示     -l 显示所有已经启动的端口     -p 显示pid

参数

enabled #开机启动
name #服务名称
state  
    started
    stopped
    restarted
    reloaded
user #启动的用户

示例

ansible web -m service -a "name=redis state=started" #启动
ansible web -m service -a "name=redis state=stopped" #关闭
ansible web -m service -a "name=redis enabled=yes" #设置开机自启动

六.cron

  定时任务

参数

day 天
disabled 禁用crontab,表现形式加#
hour 小时
job 任务
minute 分钟
month 月
name 名字,描述信息
user 用户
weekday 周
添加时名字必须不同,不加名称为None

示例

ansible web -m cron -a "minute=12 name=touchfile job='touch /tmp/xiaoqiang.txt'"# 创建
ansible web -m cron -a "name=touchfile state=absent" #删除
ansible web -m cron -a "minute=12 name=touchfile2 job='touch /tmp/xiaoqiang.txt' disabled=yes" #注释
ansible web -m cron -a "name=None state=absent" #删除名称为空的计划任务

七.user

用户分类
  1.超级管理员 root 0
  2.普通用户
           系统用户 启动一些服务或者进程,不能登录 1-999 centos7 1-499 centos6 从大到小
           登录用户 可以登录的用户 1000-65535 centos7 500-65535 centos6 uid从小到大

参数

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

示例

ansible web -m user -a "name=tom10 shell=/sbin/nologin home=/opt/tom10 uid=3000 groups=root" #创建用户,并指定用户的shell,家目录,uid,以及附加组
ansible web -m user -a "name=tom11 shell=/sbin/nologin home=/opt/tom11"
ansible web -m user -a "name=tom12 system=yes" #创建系统用户
ansible web -m user -a "name=tom12 state=absent" #删除用户,单不删除家目录
ansible web -m user -a "name=tom11 state=absent remove=yes" # 删除用户并删除用户的家目录

linux的user命令

tail /etc/passwd  查看密码文件
tail /etc/shadow  查看用户文件
id tom2  查看tom2的uid
useradd 
    -d 设置用户家目录
useradd -d /opt/tom2 tom2
    -g 设置用户的属组
useradd -g tom2 tom3 
    -G, --groups 附加组
useradd -G tom2,root tom4
    -r, --system 系统账号
useradd -r tom5 # 系统账号没有家目录
    -s, --shell #设置用户登录后的shell
useradd -s /sbin/nologin tom8
    -u, --uid UID #设置用户的id
useradd -u 2000 tom9
设置了用户的id以后,在设置用户则从最大的id开始往后数
userdel
userdel tom8 默认不删除家目录
    -r 删除用户的家目录
userdel -r tom9 删除用户并删除用户的家目录

八.group

添加用户组

groupadd -g 设置id
         -r 系统组    

用户组分类

  超级管理员组 root 0
  普通组
    系统组 1-999 centos7 1-499 centos6  uid从大到小
    登录用户组 1000-65535 centos7 500-65535 centos6 uid从小到大
参数

gid 组id
system 系统组
name 名称
state

示例

ansible web -m group -a "name=bob10 system=yes gid=5000" 创建系统组
ansible web -m group -a "name=bob11" 创建普通的组
ansible web -m group -a "name=bob11 state=absent" #删除组

猜你喜欢

转载自www.cnblogs.com/robertx/p/10822186.html