ansible模塊補充及playbook劇本簡述

主要內(nèi)容:

1.cron 定時任務(wù)模塊
2.mount 掛載模塊
3.play-book 劇本簡述

一、cron 定時任務(wù)模塊

1.模塊參數(shù):

對比如下例子:

[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:表示狀態(tài)

present 添加(默認)
absent 刪除

[root@m01 ~]# #添加定時任務(wù)
[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 ~]# #刪除定時任務(wù)
[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.模塊參數(shù)

(1)src:源 掛載的東西從哪里來
(2)path:掛載到哪里
(3)fstype:文件系統(tǒng)類型
(4)state:狀態(tài)

present:開機掛載,僅寫入到/etc/fstab中
mounted:掛載設(shè)備,同時寫入到/etc/fstab中
absent:卸載,會清理/etc/fstab中寫入的配置
unmounted:卸載設(shè)備,不會清除/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服務(wù)的劇本
[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 服務(wù)劇本
[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'

.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容