跳转至

ansibe基础知识 - moudle

帮助手册

帮助手册

a

anasible 模块

ping 模块

指定 web ping

ansible webs -m ping

shell 模块

可以执行对应的 shell 命令

ansible webs -m shell -a 'touch /tmp/ansible.txt'

command 模块

执行 shell 但是不支持特殊符号比如管道

ansible webs -m command -a 'ifcfg'

script 模块

script 执行 shell 脚本

ansible webs -m script -a '/root/1.sh'

yum 模块

yum 模块 讲解

1. 模块帮助
name                            
    httpd                       #指定要安装的软件包名称
    file://                     #指定本地安装路径(yum localinstall 本地rpm包)
    http://                     #指定yum源(从远程仓库获取rpm包)

state                         #指定使用yum的方法
    installed,present           #安装软件包
    removed,absent              #移除软件包
    latest                      #安装最新软件包

exclude=kernel*,foo*            #排除某些包
list=ansible                    #类似于yum list查看是否可以安装
disablerepo="epel,ol7_latest"   #禁用指定的yum仓库
download_only=true              #只下载不安装 yum install d
2. 安装 httpd 服务
ansible webs -m yum -a 'name=httpd state=present'
3. 卸载 httpd 服务
ansible webs -m yum -a 'name=httpd state=absent'

copy 模块

1. copy 模块 讲解

1. copy 模块帮助
src             #推送数据的源文件信息
dest            #推送数据的目标路径
backup          #对推送传输过去的文件,进行备份
content         #直接批量在被管理端文件中添加内容
group           #将本地文件推送到远端,指定文件属组信息
owner           #将本地文件推送到远端,指定文件属主信息
mode            #将本地文件推送到远端,指定文件权限信息
2. 复制本地文件到其他服务器
1. 推送文件模块
ansible webs -m copy -a "src=/etc/passwd dest=/tmp/1.txt"

2. 在推送覆盖远程端文件前,对远端已有文件进行备份,按照时间信息备份
ansible webs -m copy -a "src=/etc/passwd dest=/tmp/1.txt backup=yes"

3. 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
ansible webs -m copy -a "content='1 ' dest=/tmp/1.txt"
3. playbook
- hosts: webs
  tasks:
    - name: copy file
      copy:
        src: /root/3.txt
        dest: /root
        owner: www
        group: www
        mode: 0600
    - name: dasdas
      copy:
        content: 新添加的文件
        dest: /root/2.txt

group 用户组模块

group 用户组模块 详解

1. group 模块帮助
name            #指定创建的组名
gid             #指定组的gid
state
    absent      #移除远端主机的组
    present     #创建远端主机的组(默认)
2. 创建 www 属组
ansible web01 -m group -a 'name=www gid=666 state=present'
3. ansible-playbook
- hosts: webs
  tasks:
    - name: create www group
      group:
        name: www
        gid: 666
        state: present

user 用户模块

user 模块详解

1. user 模块帮助
name:www                    #用户名称
uid:1040                    #指定用户的uid
group:admin                 #指定用户组名称
password                     #给用户添加密码(单引号)
shell:/sbin/nologin         #指定用户登录shell
state:present  创建用户
state:absent   删除用户
create_home:no              #是否创建家目录
2. 创建 uid 为 666 属于 666 组的虚拟用户,不创建家目录
anside web01 -m user -a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no state=present'
3. ansible-playbook
- hosts: webs
  tasks:
    - name: create user www
      user:
        name: www
        uid: 666
        group: www
        shell: /sbin/nologin
        create_home: no
        state: present

file 模块

创建文件或目录的模块

1. file 模块帮助
path            #指定远程主机目录或文件信息
recurse=yes     #递归授权
state 
    directory   #在远端创建目录
    touch       #在远端创建文件
    link        #link或hard表示创建链接文件
    absent      #表示删除文件或目录
    mode        #设置文件或目录权限
    owner       #设置文件或目录属主信息
    group       #设置文件或目录属组信息
2. 创建文件
1. 创建普通文件
ansible web01 -m file -a 'path=/tmp/pas state=touch'

2. 创建普通目录
ansible web01 -m file -a 'path=/data state=touch'

3. 创建多级目录
ansible web01 -m file -a 'path=/h1/h2 state=touch'
3. ansible-playbook
- hosts: webs
  tasks:
    - name: touch file
      file:
        path: /root/file.txt
        owner: www
        group: www
        mode: "600"
        state: touch
    - name: mkdir file
      file:
        path: /root/h1/h2
        recurse: yes
        state: directory

systemd 模块

systemd、service 模块

1. systemd、service 模块帮助
name        # 定义要启动服务的名称
state       # 指定服务状态
    started     #启动服务
    stopped     #停止服务
    restarted   #重启服务
    reloaded    #重载服务
    enabled     #开机自启
2. 启动服务
1. 启动 crond 并加入开机自启
ansible webs -m service -a "name=crond state=started enabled=yes"

2. 停止 crond 并删除开机自启
ansible webs -m service -a "name=crond state=stoped enabled=no"
3. ansible-playbook
- hosts: webs
  tasks:
    - name: started nginx
      systemd:
        name: nginx
        state: started

mount 模块

mount 模块

1. mount 模块讲解
path:/mnt/dvd   挂在地点
src:/dev/sr0    挂载谁
fstype:iso9660  挂在类型
state:unmounted 卸载,但是不删除fstab中的配置
state:present   不挂载,只写入到开机自动文件中
state:mounted   挂载,并写入到开机自启文件中
state:absent    卸载,并且删除fstab中的文件
2. 挂载
ansible web1 -m mount -a "src=172.16.1.8:/data path=/data fstype=nfs opts=defaults state=mounted"

get_url 模块

get_url 模块

1. 详解
url             #指定下载地址
dest            #指定下载的目录
mode            #指定权限
checksum        #校验加密算法
2. 下载
ansible webs -m get_url -a 'url=https://wordpress.org/latest.zip dest=/tmp/' 

unarchive 模块

mount 模块

1. 模块详解
unarchive:
src:foo.tar.gz  解压包的名字
dest: /var/lib/doo 解压的路径
remote_src=yes      解压客户端上存在的压缩包
2. 解压
ansible web01 -m unzrchive -a 'src=wordpress.tar.gz dest=/data/blog remote_src=yes'

yum_repository 模块

yum_repository 模块

1. yum_repository 模块详解
yum_repository
name:epel   名称
description     描述
enable:yes     启动仓库
state:absent   删除仓库
2. 下载源
ansible web01 -m yum_repository -a "name=epel description='EPEL YUM REPO' baseurl=http://mirrors.aliyun.com/epel/7/$basearch   "

cron 模块

cron 模块

1. cron 模块详解
name:"check dirs"  定时任务注释信息
minute:"0"         分钟
hour:"12"          小时
job:""           任务具体执行的内容
state:"absent"
2. 定时任务
1. 编写定时任务
ansible web01 -m cron -a "name=ntpdate minute=*/5 hour=* job="ntpdate ntp1.aliyun.com $>/dev/null" state=absent'

2. 删除定时任务
ansible web01 -m cron -a 'name=ntpdate state=absent' 

案例

案例

1.安装nfs、安装nginx
  (1)ansible webs -m yum -a 'name=nfs-utils state=present'

2.将配置文件exports copy到nfs服务端的家目录
  (1)ansible webs -m copy -a 'src=/etc/exports dest=/root/'

3.创建组
  (1)ansible webs -m group -a 'name=ggg gid=666 state=present'

4.创建用户
  (1)ansible webs -m user -a 'name=ggg uid=666 group=666 shell=/sbin/nologin create_home=no state=present'

5.创建目录 
  (1)ansible webs -m file -a 'path=/root/hcc state=directory'

6.修改目录权限
  (1)ansible webs -m file -a 'path=/root/hcc state=directory mode=755'

7.启动
  (1)ansible webs -m service -a 'name=nfs-utils state=stopped enabled=no'
  (2)ansible webs -m systemd -a 'name=nfs-utils state=started enabled=yes'

8.客户端挂载
  (1)ansible web02 -m mount -a "src=172.16.1.7:/hcc path=/root/cch fstype=nfs opts=defaults state=mounted"

9.卸载
  (1)ansible web02 -m mount -a "src=172.16.1.7:/hcc path=/root/cch fstype=nfs opts=defaults state=absent"

10.将本地文件推送至服务器
  (1)ansible webs -m copy -a "src=/root/222.txt dest=/root/"