ansible 學習記錄 1

 多看書老是讓人進步,今天啃了一下子《奔跑吧Ansible》,重點看了第15頁的內容,雖然剛開始的以爲一點沒看明白,可是經過search 關鍵字,漸漸地理解了一點內容,如書中講的 ansible testserver -i hosts -m ping shell

-i 是ansible命令的一個選項,該選項的參數爲inventory file,即Inventory配置文件,服務器

inventory 文件的相似下面的內容:ssh

testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 \ide

ansible_ssh_user=root \測試

ansible_ssh_private_key_file= spa

經過百度搜索,得出了其實能夠經過private key 代替ssh 互信來實現anible 操控其餘主機。vagrant

最近在做自動化部署發佈.讀了一下ansible的代碼和工做流.orm

須要部署一些免密碼登陸的操做. 記錄下server

如今有兩臺機器, 一臺服務器A安裝了ansible, 一臺是服務器B須要被操做的.ip

隨便創建一個項目文件夾.

爲了方便管理, 我這樣劃分的項目 
在conf裏面放全部的配置, 包括hosts和ansible.cfg, 而後做一個軟連接到最外面. ansible.cfg的優先級將是當前目錄最高 
aansible  tree 
在ssh_keys裏面, 存放着不少服務器的私鑰. 注意是私鑰. 
ansible用了paramiko庫.

ansible.cfg 配置以下. 注意ssh_connection下面的contrl_path=./ssh_keys, 不指定的話會一直報group-readable or world-readable and thus insecure

[defaults]inventory       = ./conf/hostspull_interval   = 15sudo_user       = roottransport       = paramikomodule_lang     = Chost_key_checking = Falsesudo_exe        = sudo# SSH timeouttimeout         = 30#remote_user     = root#remote_port     = 22remote_tmp      = $HOME/.ansible/tmp

[ssh_connection]# if True, make ansible use scp if the connection type is ssh, default is sftpscp_if_ssh      = True#sftp_batch_mode= Falsecontrol_path = ./ssh_keys12345678910111213141516171819

去服務器B上生成密鑰對,

#ssh root@服務器B#ssh-keygen 若是要重命名能夠本身指定, 回車後生成密鑰對# echo ~/.ssh/id_rsa.pub >> known_hosts  這裏把生成的公鑰放入known_hosts若是本身配置了ssh_config, 關閉了known_hosts, 可能就須要寫進~/.authorized_keys裏面去了.1234

把服務器B上的私鑰回傳到ansible執行的服務器A的ssh_keys裏, 命名爲 
服務器B_ip_.key

給ssh_keys文件夾受權爲700, 必定要700, 其它的都會報錯.

創建hosts文件裏面指定server, 每一個server一行.這裏我測試就寫一行. 
[test_server] 
10.0.1.5 ansible_ssh_private_key_file=ssh_keys/10.0.1.5.key ansible_ssh_user=root

如今回到服務器A的這個ansible文件夾下測試: 
ansible test_server -m shell -a ‘ls -la /etc/hostname’

10.0.1.5 | success | rc=0 >>-rw-r--r-- 1 root root 15 Jun  4  2012 /etc/hostname

其實也能夠配置ansible的配置文件:

[defaults]

hostfile = hosts # 注意就是上面提到的文件 位於 /playbook/hosts

remote_user = vagrant 

private_key_file = .vagrant/machines/defaults/virtualbox/private_key

host_key_checking = False 

這樣的話 執行ansible命令是能夠簡化爲 

ansible testserver -m command -a uptime 

還能夠簡化(command 模塊經常使用,ansible命令將它設爲了默認模塊)

ansible testserver -a uptime 

相關文章
相關標籤/搜索