前言:
1、ansible輸出結(jié)果顏色含義:
綠色: 命令執(zhí)行成功 沒有對遠(yuǎn)程主機(jī)做任何修改
黃色: 命令執(zhí)行成功 大部分情況表示對遠(yuǎn)程主機(jī)做了改動
紅色: 命令執(zhí)行失敗
粉色: 建議進(jìn)行操作的方法
藍(lán)色: 顯示命令或劇本執(zhí)行的過程
2昏名、ansible幫助文檔如何查看:
ansible-doc -l --- 查看所有ansible模塊信息
ansible-doc -s 模塊 --- 查看指定模塊詳細(xì)說明
ansible-doc 模塊 --- 查看指定模塊更加詳細(xì)說明
一晋柱、ansible簡介:
1曙蒸、ansible 是基于sshd服務(wù)實(shí)現(xiàn)的
2鲤屡、功能簡介:
批量管理服務(wù)
批量部署服務(wù)
批量分發(fā)數(shù)據(jù)
批量采集數(shù)據(jù)信息
3老充、ansible軟件并行批量管理
二进倍、軟件特點(diǎn)
優(yōu)點(diǎn)
1土至、被管理端不需要啟動服務(wù)程序
2、被管理端不需要編寫配置文件
3猾昆、功能強(qiáng)大
缺點(diǎn):
模塊太多了
三陶因、ansible程序功能組成
1、主機(jī)清單
2垂蜗、模塊功能
3楷扬、劇本編寫
四、ansible使用前準(zhǔn)備
1贴见、基于ssh秘鑰方式烘苹,將主機(jī)秘鑰分發(fā)至個被管理主機(jī),確保管理主機(jī)能直接登錄(不用輸入密碼)
2片部、確認(rèn)epel源更新完畢
3镣衡、安裝ansible (yum install ansible -y)
==============================================================
五、ansible 主機(jī)清單配置5種方式的(/etc/ansible/hosts)
1、簡單配置
172.16.1.7
172.16.1.31
172.16.1.41
2廊鸥、分組配置
[web]
172.16.1.31
172.16.1.41
[nfs]
172.16.1.7
172.16.1.6
3望浩、符號匹配主機(jī)信息
172.16.1.1 172.16.1.2 .. 172.16.1.200
[data]
172.16.1.[30:45]
4、配置特殊變量信息
特殊變量參照官方文檔:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
[web]
172.16.1.7 ansible_user=root ansible_password=123456 ansible_port=22
[web]
web01 ansible_host=172.16.1.7 ansible_user=root ansible_password=123456 ansible_port=22
5惰说、嵌入式胚子
5.1嵌入子組
[rsync:children]
rsync_server
rsync_client[rsync_server]
172.16.1.41
[rsync_client]
172.16.1.7
172.16.1.31
5.2嵌入變量(劇本)
第二種嵌入式:嵌入變量 (劇本)
[rsync_client]
172.16.1.7
172.16.1.31
[rsync_client:vars]
ansible_user=root
ansible_password=123456
六磨德、管理多臺主機(jī)語法格式:
ansible 主機(jī)信息(可以是組) -m 模塊名稱 -a "完成動作"
舉例:
[root@web01 ~]# ansible server -m command -a "hostname"
172.16.1.41 | CHANGED | rc=0 >>
backup-41
172.16.1.31 | CHANGED | rc=0 >>
nfs01-31
[root@web01 ~]#
七、常用模塊介紹
在ansible中 使用ansible-doc <模塊名稱> 來查看單獨(dú)模塊用法
1吆视、command模塊--可批量管理主機(jī)執(zhí)行命令(默認(rèn)模塊)
常用參數(shù)
官方模塊說明:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module
參數(shù):chdir--在執(zhí)行命令操作前進(jìn)行切換目錄
[root@web01 ~]# ansible server -m command -a "chdir=/tmp pwd"
參數(shù):creates--判斷一個文件是否存在剖张,如果存在后面的命令就不會執(zhí)行
[root@web01 ~]# ansible server -m command -a "creates=/tmp/hosts touch /tmp/hosts "
參數(shù):removes--判斷一個文件是否存在,如果不存在揩环,后續(xù)的命令不會被執(zhí)行
[root@web01 ~]# ansible server -m command -a "removes=/tmp/test touch /tmp/test"
2搔弄、shell模塊--可以批量管理主機(jī)執(zhí)行命令(萬能模塊)
官方模塊說明:https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module
參數(shù)chdir:在執(zhí)行命令前進(jìn)行目錄切換
[root@web01 ~]# ansible server -m shell -a "chdir=/tmp ls"
172.16.1.41 | CHANGED | rc=0 >>
ansible_command_payload_yJt3M6
hosts
ls
172.16.1.31 | CHANGED | rc=0 >>
ansible_command_payload_G06CEK
hosts
ls
參數(shù):creates----判斷一個文件是否存在,如果存在丰滑,后續(xù)命令不會執(zhí)行
[root@web01 ~]# ansible server -m shell -a "creates=/tmp/test touch /tmp/test"
[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.31 | CHANGED | rc=0 >>
172.16.1.41 | CHANGED | rc=0 >>
參數(shù):removes--判斷一個文件是否存在顾犹,如果不存在則候命的命令不會執(zhí)行
[root@web01 ~]# ansible server -m shell -a "removes=/tmp/test chdir=/tmp ls"
172.16.1.41 | CHANGED | rc=0 >>
ansible_command_payload_RhVulC
hosts
ls
test
172.16.1.31 | CHANGED | rc=0 >>
ansible_command_payload_WVfVC2
hosts
ls
test
3、script模塊--批量執(zhí)行腳本信息
官網(wǎng)模塊說明:https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module
使用這個模塊需要注意的
1褒墨、保證本地(需要執(zhí)行腳本的設(shè)備端)有腳本文件
2炫刷、腳本有執(zhí)行權(quán)限
[root@web01 ~]# ansible server -m script -a "/web_data/install.sh"
4、yum模塊--批量部署軟件程序
官方模塊說明:https://docs.ansible.com/ansible/latest/modules/yum_module.html#yum-module
參數(shù):name--指定安裝軟件的名稱
參數(shù):state--指定軟件安裝或卸載
在這個模塊中卸載使用 removed 安裝使用installed;注意看示例
[root@web01 ~]# ansible server -m yum -a "name=ansible state=installed"
5郁妈、service模塊--批量管理服務(wù)的運(yùn)行狀態(tài)
官網(wǎng)模塊說明:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
參數(shù)
name:指定服務(wù)名稱
enabled:設(shè)置服務(wù)是否開機(jī)自動運(yùn)行 yes:開啟開機(jī)自動運(yùn)行 no:關(guān)閉開機(jī)自動運(yùn)行
state:指定服務(wù)的運(yùn)行狀態(tài)(有以下幾個狀態(tài))restarted 重啟
reloaded 重讀配置文件(部分服務(wù)可用)
stopped 關(guān)閉服務(wù)
started 開啟服務(wù)
[root@web01 ~]# ansible server -m service -a "name=sshd state=started"
6浑玛、copy模塊--將管理端的主機(jī)數(shù)據(jù)文件,分發(fā)給被管理端;將管理端目錄中的數(shù)據(jù)移動到其他目錄
官網(wǎng)模塊說明:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
參數(shù):src--指定管理端源數(shù)據(jù)
參數(shù):dest--分發(fā)到遠(yuǎn)程主機(jī)的目標(biāo)路徑下
參數(shù):owner--專屬文件之后修改文件屬主
參數(shù):group--傳輸文件之后修改文件屬組
參數(shù):mode--修改文件的讀噩咪、寫顾彰、執(zhí)行權(quán)限
[root@web01 ~]# ansible server -m copy -a "src=/tmp/rsyncd.conf dest=/tmp owner=rsync group=rsync mode=666"
參數(shù):backup--在分發(fā)傳輸文件之前,將源文件進(jìn)行備份胃碾,按照時間信息進(jìn)行備份
參數(shù)remote_src--no表示從管理端找尋數(shù)據(jù)進(jìn)行分發(fā);yes 默認(rèn)從被管理端找尋數(shù)據(jù)進(jìn)行分發(fā)
參數(shù)content--分發(fā)文件時在文件中穿件簡單信息
[root@web01 /tmp]# ansible server -m copy -a "content='xianjian' dest=/tmp/xianjian remote_src=no mode=666"
注意:在copy模塊中不能同時使用content 參數(shù)和src參數(shù)涨享,否則會出現(xiàn)互斥,報錯如下
[root@web01 /tmp]# ansible server -m copy -a "content='xianjian' src=/tmp/xinjian dest=/tmp/xianjian remote_src=no mode=666"
172.16.1.41 | FAILED! => {
"changed": false,
"msg": "src and content are mutually exclusive"
}
172.16.1.31 | FAILED! => {
"changed": false,
"msg": "src and content are mutually exclusive"
}
7仆百、fetch模塊--將遠(yuǎn)程主機(jī)數(shù)據(jù)進(jìn)行拉取
官網(wǎng)模塊說明:https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
參數(shù):src--要拉取的遠(yuǎn)程數(shù)據(jù)
參數(shù):dest--要保存本地的文件路徑
[root@web01 /tmp]# ansible 172.16.1.41 -m fetch -a "src=/tmp/xinjian dest=/tmp/"
172.16.1.41 | CHANGED => {
"changed": true,
"checksum": "d5c2fe81c8a4fc3f532ab2617e6623dc46ce85a8",
"dest": "/tmp/172.16.1.41/tmp/xinjian",
"md5sum": "295b4c158e20855fb1fc47ec8f60600f",
"remote_checksum": "d5c2fe81c8a4fc3f532ab2617e6623dc46ce85a8",
"remote_md5sum": null
}
使用這個模塊時注意src=/tmp/xinjian 不得加/;否則會出現(xiàn)如下報錯
[root@web01 /tmp]# ansible 172.16.1.41 -m fetch -a "src=/tmp/xinjian/ dest=/tmp/"
172.16.1.41 | FAILED! => {
"changed": false,
"file": "/tmp/xinjian/",
"msg": "unable to calculate the checksum of the remote file"
}
8厕隧、file模塊--修改文件屬性信息用于創(chuàng)建數(shù)據(jù)信息(文件、目錄俄周、連接文件吁讨、刪除數(shù)據(jù))
官方說明:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
path:指定路徑信息
owner:傳輸文件之后修改文件屬主權(quán)限
group:傳輸文件后修改屬組權(quán)限
mode:直接修改文件讀、寫峦朗、執(zhí)行權(quán)限
state:touch(創(chuàng)建文件)建丧、directory(創(chuàng)建目錄)、hard(創(chuàng)建硬鏈接文件) link(創(chuàng)建軟鏈接文件)甚垦、absent(刪除數(shù)據(jù))
創(chuàng)建件目錄:
[root@web01 /]# ansible 172.16.1.31 -m file -a "path=/mnt/test/ state=directory"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 65534,
"group": "nfsnobody",
"mode": "0755",
"owner": "nfsnobody",
"path": "/mnt/test/",
"secontext": "system_u:object_r:nfs_t:s0",
"size": 6,
"state": "directory",
"uid": 65534
}
[root@web01 /]#
創(chuàng)建文件:
[root@web01 /]# ansible 172.16.1.31 -m file -a "path=/mnt/test/test state=touch"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/mnt/test/test",
"gid": 65534,
"group": "nfsnobody",
"mode": "0644",
"owner": "nfsnobody",
"secontext": "system_u:object_r:nfs_t:s0",
"size": 0,
"state": "file",
"uid": 65534
}
[root@web01 /]#
刪除數(shù)據(jù)信息:
[root@web01 /]# ansible 172.16.1.31 -m file -a "path=/tmp/test state=absent"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/test",
"state": "absent"
}
9茶鹃、mount模塊--掛載模塊
官方說明:https://docs.ansible.com/ansible/latest/modules/mount_module.html#mount-module
參數(shù):src--指定要掛載數(shù)據(jù)
參數(shù):path--指定掛載點(diǎn)
**參數(shù):fstype--指定掛載后涣雕,文件系統(tǒng)類型 如:ext3、ext4闭翩、xfs挣郭、nfs **
參數(shù):state--指定動作 如:mounted(掛載)、present(永久掛載)疗韵、umounted(臨時卸載) absent(永久卸載)
動作 | 功能 | 特點(diǎn) |
---|---|---|
mounted | 掛載 | 1兑障、會立即進(jìn)行掛載操作2、可實(shí)現(xiàn)永久掛載 |
present | 掛載 | 只能永久掛載不會臨時掛載 |
unmounted | 卸載 | 只能進(jìn)行臨時卸載 |
absent | 卸載 | 1. 進(jìn)行臨時卸載 2. 進(jìn)行永久卸載 3. 掛載點(diǎn)目錄會被刪除 |
掛載:
[root@web01 /]# ansible server -m mount -a "src=172.16.1.31:/web_data path=/mnt fstype=nfs state=mounted"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/mnt",
"opts": "defaults",
"passno": "0",
"src": "172.16.1.31:/web_data"
}
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "nfs",
"name": "/mnt",
"opts": "defaults",
"passno": "0",
"src": "172.16.1.31:/web_data"
}
10蕉汪、 cron模塊--定時任務(wù)模塊
官方說明:https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module
name: 定義定時任務(wù)注釋信息
參數(shù):minute --表示分鐘信息
參數(shù):hour --表示小時信息
參數(shù):day --表示日期信息
參數(shù):month --表示月份信息
參數(shù):weekday --表示星期信息
參數(shù):job --表示定義任務(wù)信息
參數(shù):state --指定動作 如:present(創(chuàng)建定時任務(wù))流译、absent(刪除定時任務(wù))
參數(shù):disabled: 讓定時任務(wù)臨時失效
利用ansible編寫時間同步定時任務(wù):每隔5分鐘,進(jìn)行時間同步:
[root@web01 /]# ansible server -m cron -a "name='date sync' minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null'"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"date sync"
]
}
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"date sync"
]
}
刪除定時任務(wù):
[root@web01 /]# ansible 172.16.1.31 -m cron -a "name='date sync' state=absent"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
注釋定時任務(wù):
[root@web01 /]# ansible 172.16.1.31 -m cron -a "name='date sync' minute=*/5 job='ntpdate ntp1.aliyun.com &>/dev/null' disabled=yes"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"date sync"
]
}
11者疤、 group模塊--批量創(chuàng)建用戶組
官網(wǎng)說明:https://docs.ansible.com/ansible/latest/modules/group_module.html#group-module
參數(shù):name--指點(diǎn)組名
參數(shù):gid--指定gid
參數(shù):state--指定動作 present(創(chuàng)建) absent(刪除)
[root@web01 /]# ansible server -m group -a "name=tese gid=777 state=present"
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 777,
"name": "tese",
"state": "present",
"system": false
}
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 777,
"name": "tese",
"state": "present",
"system": false
}
12福澡、user模塊--批量創(chuàng)建用戶模塊
官方說明:https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module
參數(shù):name--指定用戶名稱
參數(shù):uid--指定用戶uid信息
參數(shù):group--指定屬組
參數(shù):groups--指定屬于附加組
參數(shù):password—-指定用戶密碼信息(必須密文的信息)
參數(shù):shell—-指定用戶shell信息 /sbin/nologin
參數(shù):create_home--no表示不創(chuàng)建家目錄
創(chuàng)建用戶:
[root@web01 /]# ansible server -m user -a "name=girl uid=888 group=tese shell=/sbin/nologin create_home=no"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 777,
"home": "/home/girl",
"name": "girl",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 888
}
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 777,
"home": "/home/girl",
"name": "girl",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 888
}
刪除用戶:
[root@web01 /]# ansible server -m user -a "name=girl state=absent"
172.16.1.41 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "girl",
"remove": false,
"state": "absent"
}
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "girl",
"remove": false,
"state": "absent"
}
[root@web01 /]#
ansible創(chuàng)建用戶密文密碼
**方法一:利用ansible模塊功能
ansible all -i localhost, -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"
mypassword: 指定明文密碼信息
mysecretsalt:加密計(jì)算方式(輔助加密)
[root@web01 ~]# ansible all -i localhost, -m debug -a "msg={{ '123456' | password_hash('sha512', 'mysecretsalt') }}"
localhost | SUCCESS => {
"msg": "$6$mysecretsalt$ZB9R8AirQYAXhtfhOo2qdJz52FyNI6v3L6Uc3KNRP.arBKIYpcuEyQewT5qBAHoyQFwHkW6Z551Ql.cZ53GeY0"
}
方法二:利用python模塊功能
使用這種方法需要安裝python-pip
如果安裝不上需要更新pip源,更新方法:
更新pip源:
配置方法:在文件
~/.pip/pip.conf
中添加或修改:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
==============================================================
安裝好 :yum install python-pip 后
執(zhí)行 pip install passlib
然后執(zhí)行(什么也不用改驹马,直接執(zhí)行即可) :
python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
[root@m01 ~]# pip install passlib
Collecting passlib
Downloading https://files.pythonhosted.org/packages/ee/a7/d6d238d927df355d4e4e000670342ca4705a72f0bf694027cf67d9bcf5af/passlib-1.7.1-py2.py3-none-any.whl (498kB)
100% |████████████████████████████████| 501kB 13kB/s
Installing collected packages: passlib
Successfully installed passlib-1.7.1
You are using pip version 8.1.2, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@m01 ~]# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password:
$6$pYHuK3LNv3OwCytH$MnGUAzTXLH9TdU/Xz.9u9FY2QaZfEtH5l.kEE9hID9sZPrug6fm0M3BWMVjUk81uemTHkZhtg7i982M.05x8T1
[root@m01 ~]# ansible nfs01 -m user -a 'name=test password="$6$pYHuK3LNv3OwCytH$MnGUAzTXLH9TdU/Xz.9u9FY2QaZfEtH5l.kEE9hID9sZPrug6fm0M3BWMVjUk81uemTHkZhtg7i982M.05x8T1"'
172.16.1.31 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/test",
"name": "test",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
[root@m01 ~]# ansible nfs01 -m command -a "id test"
172.16.1.31 | CHANGED | rc=0 >>
uid=1000(test) gid=1001(test) groups=1001(test)
[root@m01 ~]#