(四)ansible 通過堡壘機訪問內網伺服器
阿新 • • 發佈:2019-01-08
場景:
在ansible的使用過程中,存在這樣的場景,ansible所在的管理節點與被管理的機器需要 通過一個跳板機才能連線,無法直接連線。要解決這個問題,並不需要在 ansible裡做什麼處理,而是在ssh連線層面解決這個問題。
1,ansible伺服器公鑰至堡壘機
2,匯出堡壘機公鑰至內網機
3,配置/root/.ssh/config
Host bastion
HostName xx.xx.xx.xx
Port 7022
IdentityFile /root/.ssh/id_rsa
ProxyCommand none
User xiaoer
Host mtr_ops
HostName 192.168.4.241
ServerAliveInterval 60
TCPKeepAlive yes
#ProxyCommand connect-proxy -S 192.168.1.126:1080 %h %p #這種方法需要安裝connect-proxy,並配置ssh埠轉發:ssh -NfD 0.0.0.0:1080 bastion
ProxyCommand ssh bastion -W %h:%p # bastion對應 Host bastion
ControlMaster auto
ControlPersist 600
User opadmin
IdentityFile /root/.ssh/id_rsa_aly_opadmin
Port 22
4,配置/etc/ansible/ansible.cfg
[defaults]
transport = ssh #通訊機制.預設 值為smart,如果本地系統支援 ControlPersist技術的話,將會使用(基於OpenSSH)'ssh',如果不支援將使用'paramiko',其他傳輸選項‘local’,‘chroot’,’jail’等等
ssh_args = -o ControlMaster=auto -o ControlPersist=5d
host_key_checking = False
remote_user = root
private_key_file = /root/.ssh/id_rsa_aly_opadmin #指定用來訪問伺服器的私鑰
deprecation_warnings = False
[ssh_connection]
ssh_args = -F /root/.ssh/config #指定ssh配置檔案
pipelining = True
scp_if_ssh = True
5,hosts配置
[mtr]
x.x.x.x ansible_ssh_host=mtr_ops #mtr_ops 對應/root/.ssh/config 中的Host mtr_ops
6,測試
ansible x.x.x.x -m ping
x.x.x.x | SUCCESS => {
"changed": false,
"ping": "pong"
}