2017-02-23 87 views
0

I'm新手與這個夢幻般的自動化引擎,並具備與瓦爾文件有點問題:ANSIBLE:使用一個主機文件變量(SSH)

由momment,我必須使用通過SSH連接不keypars一個特定的用戶和密碼。

hosts文件

[all:vars] 
connection_mode1=ssh 
ssh_user1=user1 
ssh_pass1=pass1 

[serverstest] 
host1 ansible_connection=connection_mode1 ansible_ssh_user=ssh_user1 ansible_ssh_pass=ssh_pass1 

我也想包有 「」,{},但沒有作品。

如何在此參數上使用變量?

+0

爲什麼不直接定義'ansible_connection'等PARAMS在'[所有:瓦爾]'? –

回答

2

ansible_ssh_user自2.0版以來已被棄用。它變成ansible_user。見here

千萬不要以純文本形式存儲ansible_ssh_pass變量;總是使用保險庫。見Variables and Vaults

無論如何,具有mytest.inventory文件如下

[all:vars] 
ssh_user1=user1 

[serverstest] 
host1 ansible_user="{{ ssh_user1 }}" 

它的工作原理,例如

ansible -i mytest.inventory serverstest -m ping -k 

選項-k要求輸入密碼。

如果你還是要寫清單中的密碼,您可以讓密碼變量定義,並添加ansible_ssh_pass="{{ ssh_pass1 }}"

[serverstest] 
192.168.15.201 ansible_user="{{ ssh_user1 }}" ansible_ssh_pass="{{ ssh_pass1 }}" 
+0

謝謝!我只是在實驗室環境中測試可能性來實現這個解決方案。通過momment,安全已經沒有很高的優先級,但你的約會感激地接受了:) 進出口檢驗以下 '[所有:瓦爾] ssh_user1 =用戶 ssh_pass1 =密碼 [serverstest] MACHINE1 ansible_ssh_user =」 {{ssh_user1}}」 ansible_ssh_pass = 「{{ssh_pass1}}」 機2 ansible_user = 「{{ssh_user1}}」 ansible_ssh_pass = 「{{ssh_pass1}}」' – Ramos

+0

右評論: 林測試以下 '[all:vars] ssh_user1 =用戶 ssh_pass1 =密碼 [serverstest] MACHINE1 ansible_ssh_user = 「{{ssh_user1}}」 ansible_ssh_pass = 「{{ssh_pass1}}」 機2 ansible_user =「{{ssh_user1}} 「ansible_ssh_pass =」{{ssh_pass1}}「' 機器2(用戶沒有」ssh「)不起作用。但它並不重要 非常感謝您的反饋 – Ramos

+0

可能會依賴於Ansible版本。如果它小於2.0,anslibe_ssh_user是正確的。 – gile

相關問題