ansible模块补充及playbook剧本简述

主要内容:

1.cron 定时任务模块
2.mount 挂载模块
3.play-book 剧本简述

一、cron 定时任务模块

1.模块参数:

对比如下例子:

[root@backup ~]# crontab -l
#check bak file
#00 00  * * *    sh  /server/scripts/chek.sh   >/dev/null 2>&1

(1)name:注释
相当于name="check bak file"
(2)minute:分
相当于 minute="00"
(3)hour:时
相当于hour="00"
(4)day:日
相当于day="*"或者默认不写也表示 *
(5)month:月
相当于month="*"或者默认不写也表示 *
(6)weekday:周
相当于weekday="*"或者默认不写也表示 *
(7)job:命令/操作
相当于job=" sh /server/scripts/chek.sh >/dev/null 2>&1"
(8)state:表示状态

present 添加(默认)
absent 删除

[root@m01 ~]# #添加定时任务
[root@m01 ~]# ansible oldboy -m cron -a 'name="sync time"  minute="*/5"  job="ntpdate  ntp1.aliyun.com  >/dev/null  2>&1" state=present'
172.16.1.41 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "sync time"
    ]
}
172.16.1.31 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "sync time"
    ]
}
172.16.1.7 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "sync time"
    ]
}
[root@m01 ~]# #查看是否添加成功
[root@m01 ~]# ansible oldboy -a 'crontab -l'
172.16.1.7 | CHANGED | rc=0 >>
#Ansible: sync time
*/5 * * * * ntpdate  ntp1.aliyun.com  >/dev/null  2>&1

172.16.1.41 | CHANGED | rc=0 >>
#chek bak file
#00 00  * * *    sh  /server/scripts/chek.sh   >/dev/null 2>&1
#Ansible: sync time
*/5 * * * * ntpdate  ntp1.aliyun.com  >/dev/null  2>&1

172.16.1.31 | CHANGED | rc=0 >>
#Ansible: sync time
*/5 * * * * ntpdate  ntp1.aliyun.com  >/dev/null  2>&1

[root@m01 ~]# #删除定时任务
[root@m01 ~]# ansible oldboy -m cron -a 'name="sync time"  minute="*/5"  job="ntpdate  ntp1.aliyun.com  >/dev/null  2>&1" state=absent'
172.16.1.31 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
172.16.1.41 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
172.16.1.7 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@m01 ~]# #查看是否删除成功
[root@m01 ~]# ansible oldboy -a 'crontab -l'
172.16.1.41 | CHANGED | rc=0 >>
#chek bak file
#00 00  * * *    sh  /server/scripts/chek.sh   >/dev/null 2>&1

172.16.1.31 | CHANGED | rc=0 >>


172.16.1.7 | CHANGED | rc=0 >>


[root@m01 ~]# 

二、mount 挂载模块

1.模块参数

(1)src:源 挂载的东西从哪里来
(2)path:挂载到哪里
(3)fstype:文件系统类型
(4)state:状态

present:开机挂载,仅写入到/etc/fstab中
mounted:挂载设备,同时写入到/etc/fstab中
absent:卸载,会清理/etc/fstab中写入的配置
unmounted:卸载设备,不会清除/etc/fstab中写入的配置

[root@m01 ~]# #present 挂载
[root@m01 ~]#  ansible 172.16.1.8 -m mount -a 'src=nfsserver:/data  path=/data  fstype=nfs state=present'
172.16.1.8 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "nfsserver:/data"
}
[root@m01 ~]# #查看挂载情况
[root@m01 ~]# ansible 172.16.1.8 -m shell -a 'df -h|grep data'
172.16.1.8 | FAILED | rc=1 >>
non-zero return code

[root@m01 ~]# ansible 172.16.1.8  -a 'grep data /etc/fstab'
172.16.1.8 | CHANGED | rc=0 >>
nfsserver:/data /data nfs defaults 0 0

[root@m01 ~]# #使用unmounted卸载--->失败
[root@m01 ~]#  ansible 172.16.1.8 -m mount -a 'src=nfsserver:/data  path=/data  fstype=nfs state=unmounted'
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "nfsserver:/data"
}
[root@m01 ~]# #使用absent卸载
[root@m01 ~]#  ansible 172.16.1.8 -m mount -a 'src=nfsserver:/data  path=/data  fstype=nfs state=absent'
172.16.1.8 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "nfsserver:/data"
}
[root@m01 ~]# #查看卸载情况
[root@m01 ~]# ansible 172.16.1.8  -a 'grep data /etc/fstab'
172.16.1.8 | FAILED | rc=1 >>
non-zero return code
[root@m01 ~]# #使用mounted挂载
[root@m01 ~]# ansible 172.16.1.8 -m mount  -a 'src=172.16.1.31:/data path=/data  fstype=nfs  state=mounted'
172.16.1.8 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "172.16.1.31:/data"
}
[root@m01 ~]# #查看挂载情况
[root@m01 ~]# ansible 172.16.1.8  -a 'grep data /etc/fstab'
172.16.1.8 | CHANGED | rc=0 >>
172.16.1.31:/data /data nfs defaults 0 0

[root@m01 ~]# ansible 172.16.1.8 -m shell -a 'df -h|grep data'
172.16.1.8 | CHANGED | rc=0 >>
172.16.1.31:/data   19G  1.9G   17G  10% /data

[root@m01 ~]# #使用unmounted卸载
[root@m01 ~]# ansible 172.16.1.8 -m mount  -a 'src=nfsserver:/data path=/data  fstype=nfs  state=unmounted'
172.16.1.8 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "nfsserver:/data"
}
[root@m01 ~]# #查看卸载情况
[root@m01 ~]# ansible 172.16.1.8  -a 'grep data /etc/fstab'
172.16.1.8 | CHANGED | rc=0 >>
172.16.1.31:/data /data nfs defaults 0 0

[root@m01 ~]# ansible 172.16.1.8 -m shell -a 'df -h|grep data'
172.16.1.8 | FAILED | rc=1 >>
non-zero return code

[root@m01 ~]# #使用absent卸载
[root@m01 ~]# ansible 172.16.1.8 -m mount  -a 'src=nfsserver:/data path=/data  fstype=nfs  state=absent'
172.16.1.8 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dump": "0", 
    "fstab": "/etc/fstab", 
    "fstype": "nfs", 
    "name": "/data", 
    "opts": "defaults", 
    "passno": "0", 
    "src": "nfsserver:/data"
}
[root@m01 ~]# #查看卸载情况
[root@m01 ~]# ansible 172.16.1.8  -a 'grep data /etc/fstab'
172.16.1.8 | FAILED | rc=1 >>
non-zero return code

[root@m01 ~]# ansible 172.16.1.8 -m shell -a 'df -h|grep data'
172.16.1.8 | FAILED | rc=1 >>
non-zero return code

三、ansible-playbook

使用举例:配置NFS服务的剧本
[root@m01 NFS]# vim NFS.yml 
---
  - hosts: nfsserver
    tasks:
    - name: install rpcbind and nfs-utils
      shell: yum install -y rpcbind nfs-utils
    - name: configure /etc/exports
      shell: echo "/ylz  172.16.1.0/24(rw,sync,all_squash)" >>/etc/exports
    - name: make and chown  dir /ylz
      file:
        path: /ylz
        owner: nfsnobody
        group: nfsnobody
        state: directory
    - name: stop rpcbind nfs
      shell: systemctl stop rpcbind.socket  nfs
    - name: restart && enable  rpcbind  nfs
      shell: systemctl restart  rpcbind.socket  nfs  && systemctl enable  rpcbind.socket
  - hosts: nfsclient
    tasks:
    - name: install  nfs-utils
      yum:
        name: nfs-utils
        state: present
    - name: make   dir /ylz
      file:
        path: /ylz
        state: directory
    - name: mount
      mount:
        path: /ylz
        src: 172.16.1.31:/ylz
        state: mounted
        fstype: nfs
rsync 服务剧本
[root@m01 RSYNC]# vim RSYNC.yml 
---
  - hosts: rsyncserver
    tasks:
    - name: install rsync
      yum:
        name: rsync
        state: present
    - name: deltet /etc/rsyncd.conf
      file:
        path: /etc/rsyncd.conf
        state: absent
    - name: configure /etc/rsyncd.conf
      copy:
        src: /etc/ansible/RSYNC/rsyncd.conf
        dest: /etc/rsyncd.conf
    - name: make chown  /data
      file:
        path: /data
        state: directory
        owner: rsync
        group: rsync
    - name: make  /etc/rsync.password
      file:
        path: /etc/rsync.password
        state: touch
    - name: add to /etc/rsync.password
      copy:  content='rsync_backup:123456\n'  dest=/etc/rsync.password  mode='0600'
    - name: restart && enable rsyncd
      service:
        name: rsyncd
        state: restarted
        enabled: yes
  - hosts: rsyncclient
    tasks:
    - name: make chmod /etc/rsync.password
      file:
        path: /etc/rsync.password
        state: touch
    - name: add to /etc/rsync.password
      copy: content='123456\n'  dest=/etc/rsync.password   mode='0600'

.

你可能感兴趣的:(ansible模块补充及playbook剧本简述)