ansible

ansible是新出現的自動化運維工具,基於Python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。

ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。主要包括:

(1)、連接插件connection plugins:負責和被監控端實現通信;

(2)、host inventory:指定操作的主機,是一個配置文件裏面定義監控的主機;

(3)、各種模塊核心模塊、command模塊、自定義模塊;

(4)、藉助於插件完成記錄日誌郵件等功能;

(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務。


實驗環境

centos6.9_x64

server  192.168.1.128

slave   192.168.1.135


實驗軟件

ansible-2.6.8-1.el6.noarch

sshpass-1.06-1.el6.x86_64

python-crypto2.6-2.6.1-2.el6.x86_64

python-jinja2-26-2.6-3.el6.noarch


軟件安裝

rpm -ivh epel-release-6-8.noarch.rpm

sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo

yum clean all 

yum makecache 

yum install ansible* -y master/salve端操作


cp /etc/ansible/hosts /etc/ansible/hosts.bak

vim /etc/ansible/hosts

[server]              模塊名可自定義

192.168.1.135 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=1   定義ip  用戶名 端口號  密碼


cp /etc/ansible/ansible.cfg /etc/ansible/ansible.cfg.bak

sed -i 's/#host_key_checking = False/host_key_checking = False/g' /etc/ansible/ansible.cfg


ps -aux | grep ansible

Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

root      2372  0.0  0.0 103332   884 pts/0    S+   09:37   0:00 grep ansible



ansible-doc -l                            查看功能模塊 

ansible-doc -l | grep copy         查看具體功能模塊 

-m                                          添加模塊執行

-a '命令' 

ansible all                               所有主機

ansible server                         自定義分組批量執行命令

ansible all -m ping

192.168.1.135 | SUCCESS => {

    "changed": false, 

    "ping": "pong"

}              


ansible all -a 'uptime'           ls cp等均可實現

192.168.1.135 | SUCCESS | rc=0 >>

09:43:36 up 29 min,  3 users,  load average: 0.00, 0.02, 0.00


ansible server -m copy  -a  "src=/home/test.sh  dest=/tmp/ owner=root group=root mode=0755" 批量複製腳本 

ansible server -m shell -a   "/bin/sh /tmp/test.sh"                              執行腳本

ansible java   -m shell -a   "/bin/sh /tmp/test.sh"  --sudo                       sudo提權執行腳本

ansible server -m file  -a  "dest=/home/test.txt state=touch"    創建文件

ansible server -m file -a "dest=/home/test.txt state=absent"      刪除文件

ansible server -m file -a "dest=/home/tests  mode=755 owner=root group=root state=directory"  建立目錄

ansible server -m file -a "dest=/home/tests state=absent"       刪除目錄

 ansible server -m yum   -a "name=httpd"  安裝服務

ansible server -m yum   -a "name=vsftpd" 

ansible server -m service -a "name=httpd state=started/restarted/stopped enabled=yes"   遠程啓動服務

ansible server -m service -a "name=vsftpd state=started enabled=yes"


openssl passwd -1 -salt 123.com    123.com 爲用戶密碼,opnenssl生成密文密碼

ansible server -m user -a 'name=test comment="add a test user" password="$1$123.com$6Oaka602q3MP5w4ZaugbB0"'  建立用戶設置密碼

ansible server -m group -a "name=g1 gid=666 state=present system=yes"  新建組 g1位組名

ansible server -m group -a "name=g1 state=absent"                刪除組 

ansible server -m command -a "id test01" 查看用戶

ansible server -m user -a "name=test01 state=absent remove=yes"   刪除用戶


ansible server -a "uptime"  執行命令

ansible server -m raw -a "ifconfig eth0"              查看主機IP地址

ansible server -m raw -a "tail /etc/group | grep g1"  查看組

ansible java   -m setup -a "filter=ansible_eth[0-1]"  查看ip地址

ansible all  -m ping       查看主機存活情況

ansible server -m setup      系統信息收集

ansible server --list      查看分組主機

ansible test -u root -m setup  查看系統信息

ansible server   -a "rpm -qa  vsftpd"  查看安裝rpm包

ansible server   -a "lsof -i:21"      查看服務狀態

ansible server   -a "netstat -tuplna | grep vsftpd"

ansible all -a "/sbin/reboot" -f 10 --sudo -K  重啓 --sudo 爲普通用戶sodu權限

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章