ansible批量管理服務 上

  • 2019 年 10 月 3 日
  • 筆記

1 ansible簡介

1.1 ansible批量管理服務概述

(1)是基於python語言開發的自動化軟體工具
(2)是基於SSH遠程管理服務實現遠程主機批量管理
(3)並行管理,部署簡單,應用也簡單方便

1.2 ansible批量管理服務意義

(1)提高工作的效率
(2)提高工作的準確度
(3)減少維護的成本
(4)減少重複性工作

1.3 ansible批量管理服務功能

(1)可以實現批量系統操作配置
(2)可以實現批量軟體服務部署
(3)可以實現批量文件數據分發
(4)可以實現批量系統資訊收集

1.4 ansible批量管理服務特點

(1)管理端不需要啟動服務程式(no server)
(2)管理端不需要編寫配置文件(/etc/ansible/ansible.cfg)
(3)受控端不需要安裝軟體程式(libselinux-python)
(4)受控端不需要啟動服務程式(no agent=無代理)
(5)服務程式管理操作模組眾多(module)
(6)利用劇本編寫來實現自動化(playbook)

1.5 ansible批量管理服務架構

(1)連接插件(connectior plugins) :用於連接主機 用來連接被管理端
(2)核心模組(core modules) :連接主機實現操作, 它依賴於具體的模組來做具體的事情
(3)自定義模組(custom modules) :根據自己的需求編寫具體的模組
(4)插件(plugins) :完成模組功能的補充
(5)劇本(playbooks):ansible的配置文件,將多個任務定義在劇本中,由ansible自動執行
(6)主機清單(host inventory):主機清單配置資訊,定義ansible需要操作主機的範圍
(7)最重要的一點是 ansible是模組化的,它所有的操作都依賴於模組
111_看图王.png

2 ansible服務部署

2.1 ansible安裝

系統、內核版本

[root@m01 ~]#cat /etc/redhat-release  CentOS Linux release 7.5.1804 (Core)  [root@m01 ~]#uname -a  Linux m01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

安裝ansible

[root@m01 ~]yum repolist    # 查看是否有相應epel源,安裝ansible需要epel源  [root@m01 ~]yum install -y ansible  [root@m01 ~]#rpm -qa ansible  ansible-2.8.1-1.el7.noarch  # ansible版本

2.1 ansible語法格式

ansible 管理主機資訊  -m  模組名稱 -a "操作動作" 
1563888396102-e011e809-7cc9-424a-b75f-73dd5f0bb771.png

2.2 ansible主要配置文件

官方資料: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
配置文件

[root@m01 ~]#rpm -ql ansible  /etc/ansible/ansible.cfg     --- ansible配置文件  /etc/ansible/hosts           --- 主機清單文件,定義管理的主機資訊  /etc/ansible/roles           --- 角色目錄(更加規範使用ansible)

ansible配置文件

vi /etc/ansible/ansible.cfg  #ansible_become=True             --- 開啟用戶切換功能  #ansible_become_method=sudo      --- 使用什麼方式切換用戶許可權 su / sudo  #ansible_become_user=root        --- 默認切換成什麼用戶  #ansible_become_ask_pass=false   --- 是否使用密碼認證

2.3 ansible主機清單配置

ansible_become 開啟用戶sudo或su功能
ansible_become_method 選擇獲取許可權的方式 su / sudo
ansible_become_user 指定切換成什麼用戶操作
ansible_become_password 實現設置su或者sudo密碼資訊

2.3.1 方法一:編寫遠程主機地址資訊

[root@m01 ~]#vim /etc/ansible/hosts  # 最後面添加IP地址  172.16.1.7  172.16.1.31  172.16.1.41

命令檢測
所有IP地址可以用 all 來代替

root@m01 ~]#ansible all -m command -a "hostname"  172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup    172.16.1.31 | CHANGED | rc=0 >>  nfs01

也可以用逗號隔開IP地址

[root@m01 ~]#ansible 172.16.1.31,172.16.1.41,172.16.1.7 -m command -a "hostname"  172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.31 | CHANGED | rc=0 >>  nfs01    172.16.1.41 | CHANGED | rc=0 >>  backup

2.3.2 方法二:編寫服務內置變數資訊

編寫服務內置變數資訊

ansible_port=xxx      --- 指定遠程服務埠號  ansible_password=xxx  --- 指定遠程服務密碼資訊  ansible_user=xxx      --- 指定以什麼用戶遠程連接主機  ansible_host=xxx      --- 指定遠程主機地址資訊和名稱做映射,XXX=ip地址

命令檢測:
在這裡密碼,埠都默認一樣的

[root@m01 ~]#vim /etc/ansible/hosts  nfs01 ansible_host=172.16.1.31  172.16.1.41 ansible_user=root ansible_password=123456  172.16.1.7  ansible_user=root ansible_password=123456 ansible_port=22    # 命令執行  [root@m01 ~]#ansible all -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01    172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup

如果埠,密碼不一樣該怎麼用

[root@m01 ~]#vim /etc/ansible/hosts  nfs01 ansible_host=172.16.1.31  172.16.1.41 ansible_user=root ansible_password=123456  172.16.1.7  ansible_user=root ansible_password=654321 ansible_port=52113    # 命令執行  [root@m01 ~]#ansible all -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01    172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup

2.3.3 方法三:編寫服務主機組資訊

編寫服務主機組資訊

[root@m01 ~]#vi /etc/ansible/hosts  [ichn]  nfs01 ansible_host=172.16.1.31  172.16.1.41 ansible_user=root ansible_password=123456  [web]  172.16.1.7  ansible_user=root ansible_password=123456 ansible_port=22  

命令檢測
主機組所有名稱可以用 all 代替

[root@m01 ~]#ansible all -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01    172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup

主機組所有名稱之間可以用逗號隔開

[root@m01 ~]#ansible ichn,web -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01    172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup

2.2.4 方法四:編寫服務主機子組資訊

編寫服務主機子組資訊
children:是固定的

[root@m01 ~]#vim /etc/ansible/hosts  [nfs:children]  nfs_server  nfs_client  [nfs_server]  nfs01 ansible_host=172.16.1.31  [nfs_client]  172.16.1.41 ansible_user=root ansible_password=123456  172.16.1.7  ansible_user=root ansible_password=12345 ansible_port=22

命令檢測
所有主機組,子組都可以用 all 來代替

[root@m01 ~]#ansible all -m command -a "hostname"  172.16.1.41 | CHANGED | rc=0 >>  backup    172.16.1.7 | CHANGED | rc=0 >>  web01    nfs01 | CHANGED | rc=0 >>  nfs01

所有主機組,子組都可以用 nfs 來代替

[root@m01 ~]#ansible nfs -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01    172.16.1.7 | CHANGED | rc=0 >>  web01    172.16.1.41 | CHANGED | rc=0 >>  backup

可以單獨啟動其中一個子組

[root@m01 ~]#ansible nfs_server -m command -a "hostname"  nfs01 | CHANGED | rc=0 >>  nfs01

2.2.5 方法五:編寫配置主機清單變數資訊

配置主機清單變數資訊

[root@m01 ~]#vim /etc/ansible/hosts  [nfs:vars]  pass=123456  port=873  [nfs]  172.16.1.31  172.16.1.41    [nfs_client]  172.16.1.41  [nfs_client:vars]  ansible_user=root  ansible_password=123456

命令檢測
所有主機清單變數都可以用 all 來代替

[root@m01 ~]#ansible all -m command -a "hostname"  172.16.1.31 | CHANGED | rc=0 >>  nfs01    172.16.1.41 | CHANGED | rc=0 >>  backup

可以用變數的主機組來代替

[root@m01 ~]#ansible nfs -m command -a "hostname"  172.16.1.31 | CHANGED | rc=0 >>  nfs01    172.16.1.41 | CHANGED | rc=0 >>  backup

2.4 ansible遠程連接操作異常說明

(1)查看主機清單配置文件是否正確
(2)利用SSH命令直接連接,連接不上查看有沒有公鑰  密碼正不正確  指定用戶正不正確  防火牆是否阻止
(3)遠程管理一直報錯,只要老的ansible遠程會話存在

[root@m01 /]#ps -ef|grep ssh  # SSH遠程服務進程(實現客戶端遠程連接)  root       1211      1  0 09:49 ?        00:00:00 /usr/sbin/sshd -D  # SSH遠程連接會話進程(維持連接會話)  root       2179   1211  0 15:16 ?        00:00:00 sshd: root@pts/1,pts/2

3 ansible服務模組應用方法

3.1 command模組(默認模組,不加模組會默認使用command)

command – Execute commands on targets:對目標主機執行命令
Synopsis:模組詳細說明
(1)模組多個參數要用空格分隔
(2)使用commad模組一些特殊符號資訊不能使用,如果非要執行請使用shell模組
像變數$HOME和"<",">","|",";"和"&"將不起作用
Parameters:模組的參數

chdir 在運行命令之前,先切換指定目錄(遠程主機)
creates 在執行命令前,判斷遠程主機指定文件是否存在,如果存在,命令不執行
removes 在執行命令前,判斷遠程主機指定文件是否存在,如果存在,命令繼續執行
free_form 在使用這個模組是, -a後面必須輸入一個合法linux命令

參數演示
chdir:在運行命令之前,先切換指定目錄(遠程主機)

[root@m01 ~]#ansible 172.16.1.41 -m command -a "pwd"  172.16.1.41 | CHANGED | rc=0 >>  /root    [root@m01 ~]#ansible 172.16.1.41 -m command -a "chdir=/tmp pwd"  172.16.1.41 | CHANGED | rc=0 >>  /tmp

creates:在執行命令前,判斷遠程主機指定文件是否存在,如果存在,命令不執行

[root@m01 ansible]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf touch /etc/rsyncd.conf"  172.16.1.41 | SUCCESS | rc=0 >>  skipped, since /etc/rsyncd.conf exists      # 文件已存在,不會執行後續命令    [root@m01 ~]#ansible 172.16.1.41 -m command -a "creates=/tmp/test.txt touch /tmp/test.txt"   [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need  to use command because file is insufficient you can add 'warn: false' to this command task or set  'command_warnings=False' in ansible.cfg to get rid of this message.    172.16.1.41 | CHANGED | rc=0 >>  # 文件沒有,執行後續命令創建文件  [root@backup tmp]#ll  total 2  -rw-r--r-- 1 root root   0 Jul 24 09:52 test.txt

3.2 shell模組(萬能模組 )

shell – Execute shell commands on targets:在遠程目錄主機執行命令
Parameters:模組的參數 和command參數一樣

chdir 在運行命令之前,先切換指定目錄(遠程主機)
creates 在執行命令前,判斷遠程主機指定文件是否存在,如果存在,命令不執行
removes 在執行命令前,判斷遠程主機指定文件是否存在,如果存在,命令繼續執行
free_form 在使用這個模組是, -a後面必須輸入一個合法linux命令

參數演示
shell識別 > 

[root@m01 ~]#ansible 172.16.1.41 -m shell -a "echo 123456 >/tmp/test.txt"  172.16.1.41 | CHANGED | rc=0 >>    [root@backup tmp]#cat test.txt  123456

3.3 script模組(腳本模組)

Runs a local script on a remote node after transferring it:遠程批量執行本地腳本資訊
shell模組遠程執行腳本
第一步:編寫一個腳本

[root@m01 scripts]#vim yum.sh  #!/bin/bash  yum install -y htop 

第二步:遠程傳輸腳本

[root@m01 scripts]#ansible 172.16.1.41 -m copy -a "src=/server/scripts/yum.sh dest=/server/scripts"

第三步:批量執行腳本文件

[root@m01 scripts]#ansible 172.16.1.41 -m shell -a "sh /server/scripts/yum.sh"

script 遠程執行腳本
第一步:編寫腳本

[root@m01 scripts]#vim yum.sh  #!/bin/bash  yum install -y htop 

第二步:遠程執行腳本

[root@m01 scripts]#ansible 172.16.1.41 -m script -a "/server/scripts/yum.sh"  # scripy模組可以省略sh

相比shell模組來講,script不用將腳本傳輸到遠程主機,腳本本身不用進行授權,即可利用script模組執行,不需要使用sh,更加簡潔,高效。

3.4 copy模組 把本地文件推送到遠程主機

copy:可以將本地數據批量拷貝到遠程主機,可以將遠程主機數據進行移動操作(並不是拉取)

src  指定本地要推送的源數據資訊
dest 指定保存數據目錄路徑資訊
mode 數據推送後修改數據許可權
owner 修改數據推送後的所屬用戶資訊
group 修改數據推送後的所屬組資訊
remote_src 指定源為遠程主機路徑資訊
backup 將數據進行備份
content 在指定遠程主機生成有數據的文件,指定文件內容
directory_mode 遞歸設定目錄的許可權,默認為系統默認許可權
forces 如果目標主機包含該文件,但內容不同,如果設置為yes,則強制覆蓋。
如果為no,則只有當目標主機的目標位置不存在該文件時,才複製。默認為yes。
others 所有的file模組里的選項都可以在這裡使用

使用copy 模組,將/ichn/test.txt文件傳輸到各個伺服器,許可權修改為666 屬主屬組為ichn

[root@m01 ichn]#ansible all -m copy -a "src=/ichn/test.txt dest=/backup mode=666 owner=ichn group=ichn"

檢查結果

[root@m01 ichn]#ansible all -m shell -a "ls -l /backup/test.txt"  172.16.1.41 | CHANGED | rc=0 >>  -rw-rw-rw- 1 ichn ichn 0 Jul 20 14:48 /backup/test.txt    172.16.1.31 | CHANGED | rc=0 >>  -rw-rw-rw- 1 ichn ichn 0 Jul 24 11:05 /backup/test.txt

ansible all -m copy -a "src=/ichn/ =/backup mode=666 owner=ichn group=ichn"
ansible all -m copy -a "src=/ichn dest=/backup mode=666 owner=ichn group=ichn"
說明: 複製目錄是遵循rsync複製目錄原理 目錄後面有/ 和 沒有/有區別
實現數據備份

ansible nfs -m copy -a "src=/ichn/ichn.txt dest=/backup backup=yes"

批量備份:

ansible nfs -m copy -a "src=/backup/ichn.txt dest=/backup/ichn.txt.bak remote_src=yes"

批量還原:

ansible nfs -m copy -a "src=/backup/ichn.txt.bak dest=/backup/ichn.txt remote_src=yes"

生成一個有數據的文件

ansible nfs_client -m copy -a "content="ichn123" dest=/etc/rsync.password mode=600"

3.5 fetch模組 拉取數據操作

參數:

dest 將遠程主機拉取過來的文件保存在本地的路徑資訊
src 指定從遠程主機要拉取的文件資訊,只能拉取文件
flat 默認設置為no,如果設置為yes,將不顯示172.16.1.41/etc/資訊

參數演示:dest,src

[root@m01 tmp]#ansible 172.16.1.41 -m fetch -a "src=/etc/hosts dest=/tmp"    [root@m01 tmp]#ll  total 0  drwxr-xr-x 3 root root 17 Jul 24 21:27 172.16.1.41  drwx------ 2 root root  6 Jul 23 08:20 vmware-root

flat=yes

[root@m01 tmp]#ansible 172.16.1.41 -m fetch -a "src=/etc/hosts dest=/tmp/ flat=yes"    [root@m01 tmp]#ls  hosts  vmware-root

3.6 file模組 修改遠程主機數據屬性,創建和刪除遠程主機數據資訊

參數:

path 指定遠程主機上已有的一個文件數據
mode 修改數據許可權數值
owner 修改屬主
group 修改屬組
src 指定要創建軟鏈接的文件資訊
recurse recurse=yes 將目錄及目錄里文件的許可權遞歸修改 recurse=no 不遞歸





state
state=directory 創建目錄,可以遞歸創建目錄
state=touch 創建文件:如果路徑不存在將創建一個空文件
state=link 創建軟鏈接
state=hard 創建硬鏈接
state=absent 刪除數據
state=file 檢查文件是否存在

參數演示:mode owner group

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/test.txt mode=666 owner=ichn group=ichn"    [root@backup tmp]#ll -d test.txt  -rw-rw-rw- 1 ichn ichn 7 Jul 24 10:07 test.txt

state=directory:創建目錄

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/yang state=directory"    [root@backup tmp]#ll  total 8  drwxr-xr-x 2 root   root     6 Jul 24 21:52 yang

遞歸創建目錄

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/yang/yang1/yang2 state=directory"    [root@backup tmp]#tree yang  yang  └── yang1      └── yang2    2 directories, 0 files

state=touch:創建文件,如果路徑不存在將創建一個空文件

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/ceshi.txt state=touch"

state=link:創建軟鏈接

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "src=/tmp/test.txt  path=/backup/test_soft_link.txt state=link"

state=hard:創建硬鏈接

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "src=/tmp/test.txt  path=/backup/test_hard_link.txt state=hard" 

state=absent:刪除數據操作

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/backup/test_hard_link.txt state=absent" 

state=file:檢查文件是否存在

[root@m01 tmp]#ansible 172.16.1.41 -m file -a "path=/tmp/ceshi.txt state=file" 

recurse=yes:遞歸修改許可權

[root@m01 ~]#ansible 172.16.1.41 -m file -a "path=/tmp/yang/ mode=700 owner=ichn group=ichn recurse=yes"

3.7 yum模組 批量安裝軟體模組

參數:

name  指定要安裝的軟體名稱




state
state=installed

安裝軟體
state=present
state=latest 更新軟體
state=removed

移除卸載軟體
state=absent 

參數演示:absent installed removed

[root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=absent"    --- 卸載軟體  [root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=installed" --- 安裝軟體  [root@m01 tmp]#ansible 172.16.1.41 -m yum -a "name=htop state=removed"   --- 卸載軟體

3.8 service模組 批量管理服務啟動狀態

參數

name 管理哪個服務名稱



state
state=reloaded 平滑重啟服務
state=restarted 重啟服務
state=started 啟動服務
state=stopped 停止服務


enabled
enabled=no 開機不運行
enabled=yes 開機自動運行

參數演示:restarted started stopped enabled=no enabled=yes

[root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=stopped"     停止服務  [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=started"     啟動服務  [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond state=restarted"   重啟服務  [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond enabled=no"            開機不啟動服務  [root@m01 tmp]#ansible 172.16.1.41 -m service -a "name=crond enabled=yes"           開機啟動服務

3.9 corn模組 批量部署定時任務

參數:

minute  分 Minute when the job should run ( 0-59, , /2, etc )
hour    時 Hour when the job should run ( 0-23, , /2, etc )
day     日 Day of the month the job should run ( 1-31, , /2, etc )
month   月 Month of the year the job should run ( 1-12, , /2, etc )
weekday 周 Day of the week that the job should run ( 0-6 for Sunday-Saturday, *, etc )
job      工作:要做的事情
name     定義定時任務的描述資訊


disabled
disabled=no 取消注釋
disabled=yes 添加註釋


state
state=absent 刪除定時任務
state=present 創建定時任務

要求: 每隔五分鐘,進行時間同步

ansible 172.16.1.41 -m cron -a "minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"

要求: 每周五,晚上10:30, 需要去大保健

ansible 172.16.1.41 -m cron -a "name='大保健' minute=30 hour=22 weekday=5 job='make shufu &>/dev/null'"  說明: 定時任務配置時.需要添加name注釋資訊,否則會出現反覆創建相同定時任務

注釋定時任務 刪除定時任務 創建定時任務

ansible 172.16.1.41 -m cron -a "name='大保健03' state=absent"   --- 刪除指定定時任務  ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=yes"  ansible 172.16.1.41 -m cron -a "name='大保健02' job='make shufu &>/dev/null' disabled=no"

3.10 user模組 批量創建創建用戶

參數

name 創建的用戶名稱
password 設置用戶密碼資訊 必須設置為密文
create_home yes 表示創建家目錄 no 不創建家目錄
shell 指定用戶登錄方式  shell=/sbin/nologin
group 指定用戶屬於哪個組 主要組
groups 指定用戶屬於哪個組 附屬組
uid 指定用戶uid數值
state=absent 刪除用戶
remove=yes 當與state=absent連用的時候,徹底刪除指定用戶。
remove=no 不刪除家目錄

參數演示:name create_home=no shell=/sbin/nologin  group  groups uid state=absent

# 批量創建虛擬用戶  ansible 172.16.1.41 -m user -a "name=alex create_home=no shell=/sbin/nologin"  # 指定用戶所屬組  ansible 172.16.1.41 -m user -a "name=alex group=ichn groups=oldgirl"  # 指定用戶uid  ansible 172.16.1.41 -m user -a "name=alex uid=2000"  # 刪除用戶  ansible 172.16.1.41 -m user -a "name=alex state=absent"

徹底刪除指定用戶:state=absent remove=yes

[root@m01 ~]#ansible 172.16.1.41 -m user -a "name=alex group=alex state=absent remove=yes"

給用戶設置密碼

ansible 172.16.1.41 -m user -a 'name=oldbaby password="$6$oldgirl$kAUTXVC2z1agr1HlmpFe9abFhWKwJ1fNyg64F95U3rVumwQfqOuhV3YkyZU9.H79TChzIKn5epl5M18B199qV1"'

密碼生成方式
方法一:明文加密,演算法生成密文

ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"  # mypassword    --- 明文密碼資訊  # sha512        --- 明文轉換為密文加密方法  # mysecretsalt  --- 用什麼做演算法依據生成密文資訊

實踐演示

[root@m01 ~]#ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'ichn123') }}"  localhost | SUCCESS => {      "msg": "$6$ichn123$W3jkmkkVTr.9UStm4S50RT2uIEjB/4GEtaAeVCSZ..uWVN1YGxHvluss9JVfAPV0gSJoGn1qAfxGyttIsTjcz0"  }

方法二:centos7無法使用
mkpasswd --method=sha-512
方法三:利用python模組功能
直接安裝

[root@m01 ~]#yum install -y python-pip

優化pip源

[root@m01 ~]#mkdir ~/.pip/  [root@m01 ~]#vim ~/.pip/pip.conf  [global]  index-url = https://mirrors.aliyun.com/pypi/simple/    [install]  trusted-host=mirrors.aliyun.com    python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"  [root@m01 ~]#pip install passlib    # 安裝

3.11 mount 批量掛載模組

參數 參數說明
fstype 指定掛載文件類型 fstype=nfs
opts  設定掛載的參數選項資訊opts=ro opts=rw
path  掛載點路徑資訊
src   要被掛載的存儲設備(目錄資訊)




state 
state狀態參數
state =unmounted 立即卸載,/etc/fstab文件中不會永久卸載
state=absent   立即卸載,/etc/fstab文件中永久卸載
state=present  只能實現開機自動掛載
state=mounted 將掛載資訊添加到/etc/fstab文件,開機自動掛載
實現立即掛載

參數演示:src  path fstype=nfs state=mounted state=unmounted

mount -t nfs 172.16.1.31:/data  /mnt        # /mnt掛載到172.16.1.31:/data  # 立即掛載 並且添加在/etc/fstab文件  ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=mounted"  # 立即卸載,/etc/fstab文件中不會永久卸載  ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data path=/mnt fstype=nfs state=unmounted"

3.12 模組參數 ansible顏色提示資訊說明

哪些是重要參數
(1)通過其他人博文
(2)看官網網站文檔中,有紅色字體標記的
(3)看官網網站文檔中,舉例配置中參數使用頻次  
ansible幫助資訊查看方法
(1)查看ansible所有模組:ansible-doc -l
(2)查看ansible模組參數資訊:ansible-doc -s cron(模組可以換)
(3)查看ansible模組詳細資訊:ansible-doc cron(模組可以換)   
ansible顏色提示資訊說明

黃色 對遠程主機做出了改動
綠色 查詢資訊,對遠程主機沒有做改動
紅色 遠程執行命令出錯了
紫色 警告資訊(建議資訊)
藍色 命令執行的過程

Tags: