一千萬個為什麽

搜索

使用Ansible連接到Windows機器



我試圖簡單地連接到一臺機器並運行win_ping模塊。

網上有幾個教程概述了幾個關鍵步驟:

  • Create a directory ./windows
  • Create a file hosts in ./windows
  • Create a directory ./windows/group_vars
  • Create a file windows.yml in ./windows/group_vars
  • Insert several variables with your Windows credentials into windows.yml

問題是這些實際上都沒有指定將這些相對於'。'放在哪裏。我試過把這個結構放在/etc/ansible/中,不幸的是,它看起來像Ansible根本沒有選擇這個。我也嘗試過移動結構,但沒有運氣。

我如何讓Ansible引用我創建的目錄?我相信 ansible.cfg 是有道理的,但我只能看到如何將其指向默認庫存文件。有趣的是,甚至在基本主機文件中添加了一個名為“windows”的組,並在那裏調用了與該組相關的組合 - 它表示沒有找到主機。我還取消了ansible.cfg中的庫存位置以指向/etc/ansible/hosts ,並且它仍然顯示調用 ansible windows時找不到主機-m ping (或 win_ping )。

轉載註明原文: 使用Ansible連接到Windows機器

一共有 2 個回答:

Ansible在兩個地方查找 group_vars 子目錄:

  • playbook目錄 - 包含您運行的Playbook的目錄
  • 清單目錄 - 運行時使用 -i 選項指定的目錄,或默認目錄(通常為/etc/ansible/usr /本地/ etc/ansible </代碼>)

在大多數情況下,您可以將 group_varsplaybook.yml 一起放置。

您發布的示例使用第二種方法,因此要引用需要向該命令添加 -i 的文件:

ansible windows -m ping -i ./windows

關於Ansible最佳實踐,有幾個GitHub回購,我發現 enginyoyen/ansible-best-練習不僅有助於您的案例,而且還可以幫助您了解全面Ansible項目中的劇本/庫存/角色/任務差異。

一些亮點:(/引用項目的主目錄)

  • Your variables do indeed go in /group_vars/
    • /group_vars/windows.yml will be automatically read by hosts you assign to the windows group
    • /group_vars/db.yml will be automatically read by hosts you assign to the db group and so on
  • You may also include files with names like /host_vars/win_host_1.yml which will be read only by that host
  • The groups from /group_vars/ are defined in your inventory files (plural)

    [windows]
    win_host1.example.com
    win_host_2.example.com
    
  • Inventory files have names like /production.yml and /staging.yml to help ensure code in different states of readiness is deployed to (ideally) identical environments.

其他亮點不直接回答你的問題

  • A role is a file made of the tasks required to do something, like install nginx. They have names like /roles/install_nginx.yml
  • Playbooks have names like /playbooks/win_hosts.yml which do nothing but match the groups from your inventory files with the roles you want hosts in those groups to run.
  • When you run ansible-playbook your $HOME should be /playbooks/ so that the different inventory files are referred as ansible-playbook -i ../production.yml win_hosts.yml
  • This is also the place to put a custom ansible config file, /playbooks/ansible.cfg
  • There's also a way to manage external roles from Ansible Galaxy or GitHub and keep them separate from custom roles you built, but still in your project directory instead of /etc/ansible/roles/ where ansible-galaxy puts them by default.
  • A nice little bash script to install Ansible