ansible模块补充

1.fetch模块, 将远程机器上的文件拉取到本地,以ip或者主机名生成目录,并保留原来的目录结构,与copy模块的功能相反.

   主要参数 : dest  --  目标地址

        src -- 源地址

   例子 : ansible web -m fetch -a "dest=/tmp src=/var/log/cron"     #表示把远程主机上/var/log/cron下的文件copy到本机的/tmp下.

2.yum模块

   首先,linux自带的yum有其自身的功能 : 

     linux中yum源的配置格式 :

[epel]                                                   # 名称
name=Extra Packages for Enterprise Linux 7 - $basearch   # 描述信息
baseurl=http://mirrors.aliyun.com/epel/7/$basearch       # yum源的地址
failovermethod=priority
enabled=1                                                # 指定yum源是否可用,1代表可用,0代表不可用
gpgcheck=0                                               # 是否检查gpgkey文件,0代表不检查,1代表的检查
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7      

    yum grouplist        #查看包组
    yum groupinstall   # 安装包组

    查看包安装状态 : 

      yum list|grep redis @代表安装成功
      rpm -qa 查看所有的包
      rpm -ql 查看包安装生成的文件

    然后看一下ansible中的yum模块所带的参数 : 

disable_gpg_check     # 是否要检查key
disablerepo           # 禁用repo
enablerepo            #启用repo
name                  # 包名
state                 # 状态 installed removed

示例 :

ansible web -m yum -a "name=python2-pip"             # 安装一个包
ansible web -m yum -a "name='@Development Tools'"    # 安装包组
ansible web -m yum -a "name=redis,python2-pip"       # 同时安装多个包
ansible web -m yum -a "name=redis state=absent"      # 卸载

3.pip模块

  linux中自带python2版本,其pip也有自带的功能 : 

pip list              #查看所有的python第三方包
pip freeze > a.txt    # 导出
pip install -r a.txt  # 安装

  ansible中的pip模块参数 :

requirements         #导出的文件
name                 # 包名
virtualenv           # 虚拟环境
ansible web -m pip -a "name=django==1.11.18" # 安装 

4.service模块

  linux中自带的一些服务 :

ps -ef|grep redis    # 查看进程
ss -tnlp             #查看端口信息
# 启动服务
systemctl start redis  centos7
service redis start    centos6
# 开机自启动
systemctl enable redis centos7
chkconfig redis on     centos6

   ansible中的service模块 :

enabled   # 设置开机自启动
name      # 名称
state :
	started
	stopped
	restarted
	reloaded

示例 :  

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 state=started enabled=yes"  # 启动并设置开机自启动

5.cron模块

  linux中的定时任务 :

crontab -l      # 查看计划任务
crontab -r      # 删除所有的计划任务
crontab -e      # 编辑计划任务

 ansible中的cron模块 :

day         # 天
hour        # 小时
job         #任务
minute      #分钟
month       # 月
name        #名字,描述信息,不可以重复
state       # 状态
user        # 执行计划任务的用户
weekday     # 周
disabled    # 禁止  

示例 :

ansible web -m cron -a "minute=21 job='touch /tmp/cron.txt' name=touchfile" # 设置计划任务
ansible web -m cron -a "minute=23 job='touch /tmp/cron.txt' name=touchfile4 disabled=yes" # 禁用计划任务,表现为加注释
ansible web -m cron -a "name=touchfile4 state=absent" # 删除计划任务

6.user模块

  linux中的useradd参数 :

 -d    # 指定家目录
 -g    # 组id
 -G, --groups GROUPS  # 附加组
 -r, --system         # 创建系统用户
 -s, --shell SHELL    # 登陆shell
 -u, --uid UID        #用户id

示例 :  

   useradd -s /sbin/nologin -u 2000 -d /opt/wusir 用户名           #创建用户,指定用户的登陆shell,id,家目录
   useradd -s /sbin/nologin -G root,wusir  -d /opt/wusir2 用户名   #指定附加组,最大的后面+1
   useradd -r 用户名       # 创建系统用户,从999倒序

   删除用户 :

    userdel 用户名        # 删除用户
    userdel -r 用户名    # 删除用户并删除用户的家目录

  ansible中的user模块 :

group    # 组
groups   #附加组
home     #家目录
name     #用户名
password #密码
shell    #登陆shell
remove   # 删除用户并删除用户的家目录
state    # 状态
system   #系统用户
uid      # 用户id  

示例:

ansible db -m user -a "name=用户名 shell=/sbin/nologin home=/opt/f1 uid=2000 group=root" # 创建用户,并指定用户的家目录,登陆shell,uid,组
ansible db -m user -a "name=用户名 system=yes"       #创建系统用户
ansible db -m user -a "name=用户名 state=absent"     # 删除用户
ansible db -m user -a "name=用户名 state=absent remove=yes"  # 删除用户并删除用户的家目录

7.group模块

  linux中一些关于组的操作 :      

   groupadd   #创建组

   groupdel   #删除组
     -g 指定组的id
     -r 指定系统组

  ansible中的group模块参数:

gid    #组的id
name   # 组名
state  #状态
system #系统组

示例 : 

ansible db -m group -a "name=canglaoshi"             #创建普通组
ansible db -m group -a "name=wutenglan system=yes"   # 创建系统组
ansible db -m group -a "name=wutenglan state=absent" # 删除组
原文地址:https://www.cnblogs.com/wangtaobiu/p/10684382.html