自動化運維系列 ansible

2021-09-29 20:35:45 字數 3562 閱讀 4355

前言

ansible是一款自動化運維工具,它基於python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程式部署、批量執行命令等功能。

一、命令集

ansible:日常或臨時事務使用,為一次性操作。

命令格式:ansible [options]

ansible-pull:ansible的另一種工作模式(預設使用push模式),通常大批量場景下使用。

命令格式:ansible-pull [options] [playbook.yml]

ansible-doc:ansible的模組文件說明,類似linux的man命令。

命令格式:ansible-doc [options] [module...]

ansible-playbook:ansible的劇本執行命令,可以執行事先編排好的任務集,此命令日常使用率最高。

命令格式:ansible-playbook

ansible-vault:主要用於配置檔案加密與解密,可以加密編寫playbook.yml檔案中的敏感資訊。

命令格式:ansible-vault [create|decrypt|edit|encrypt|rekey|view] [--help] [options] file_name

ansible-console:ansible提供的互動式工具,可以使用它模擬shell一樣使用ansible的內建命令

二、ansible的安裝與基本配置

可以使用yum源安裝,本例使用了阿里雲的yum源安裝

root# yum install ansible
1、修改配置檔案

# 修改ansible的配置檔案

root# vim /etc/ansible/hosts

[web]

192.168.0.10

192.168.0.11 hostname=node1

192.168.0.12 hostname=node2 ansible_ssh_user=root ansible_ssh_pass="123456"

[db]

dbserver1

dbserver2

# 以上的注釋

# [web]為主機組的別稱,代表一組主機:

如 ansible web -m ping 將會在web中的三颱主機中執行ping命令

2、使用ansible命令

執行shell

# 在web組中執行ping命令

root# ansible web -m ping # -m 為ansible預設的模組命令

# 執行所以主機的ping命令

root# ansible all -m ping

# 通過shell來執行ifconfig檢視網絡卡

root# ansible web -m shell -a "ifconfig ens33" # 通過shell將可以使用變數、管道、重定向等功能

# 以raw模組來執行ls命令

root# ansible web -m raw -a "ls"

# 本地指令碼傳送至遠端節點執行

root# ansible test -m script -a create_user.sh

# 使用-m copy拷貝本機檔案test.txt到所有主機的/root/路徑中

root# ansible all -m copy -a "src=/root/test.txt dest=/root/"

# copy並重命名

root# ansible all -m copy -a "src=/root/test.txt dest=/root/test2.txt"

# copy並設定所屬使用者與讀寫許可權,backup=yes 如目標存在同名檔案且內容不同時將自動備份

root# ansible web -m copy -a "src=/root/1.t dest=/root owner=root group=root mode=644 backup=yes force=yes"

使用者管理

# 新增使用者

root# ansible web -m user -a "name=gordon"

root# ansible web -m user -a "name=gordon uid=888"

# 刪除使用者

root# ansible web -m user -a "name=gordon state=absent"

# 建立group

root# ansible web -m group -a "name=admin gid=8888"

# 刪除group

root# ansible web -m group -a "name=admin state=absent"

文件管理

# 建立檔案

root# ansible web -m file "dest=/root/1.txt mode=660 state=touch"

# 修改檔案所屬與許可權

root# ansible web -m file "dest=/root/1.txt mode=660 owner=root group=root"

# 刪除檔案

root# ansible web -m file "dest=/root/1.txt state=absent"

# 新建目錄

root# ansible web -m file "dest=/root/test mode=750 state=directory"

# 刪除目錄

root# ansible web -m file "dest=/root/test state=absent"

軟體管理

# 使用yum安裝httpd

# 解除安裝軟體

# 設定開機啟動

# 開啟服務

# 重啟服務

# 停止服務

root# ansible web -m service -a "name=httpd state=stopped"

任務管理

# 配置定時任務

root# ansible test -m cron -a "name='date' minute='0' hour='5,2' job='date > /root/time'"

待續。。。

SQL Server 自動化運維系列

本系列為sql server自動化運維的一些操作技巧點,所有內容都是根據日常運維過程中最經常遇到的問題,並為此形成了一些自動化運維的方式,皆為原創.供部分dba和開發人員瀏覽借鑑,所應用平台基於微軟server平台,所利用技術為power shell,所關注的點為sql server.1 sql s...

SQL Server 自動化運維系列

本系列為sql server自動化運維的一些操作技巧點,所有內容都是根據日常運維過程中最經常遇到的問題,並為此形成了一些自動化運維的方式,皆為原創.供部分dba和開發人員瀏覽借鑑,所應用平台基於微軟server平台,所利用技術為power shell,所關注的點為sql server.1 sql s...

自動化運維ansible

sever1 172.25.60.1 server2 172.25.60.2 server3 172.25.60.3 etc ansible ansible.cfg 主配置檔案,配置ansible工作特性 etc ansible hosts 主機清單 etc ansible roles 存放角色的目...