[英]User defined in the inventory is not respected
我是 Ansible 的新手,我正在嘗試使用 VirtualBox 從我的主 PC 到我的 PC 上的兩個虛擬機創建一個簡單的空文件。 兩個虛擬機有 2 個適配器(一個 NAT 網絡,因此它們可以看到彼此並具有不同的 IP,第二個適配器是 Host only 適配器)。 他們有 SSH,我的主 PC 可以通過 SSH 連接,並且可以使用 ping 命令 ping 虛擬機。 我還創建了名為myhosts的庫存文件:
[servers]
user1 ansible_host=192.168.56.4
user2 ansible_host=192.168.56.3
這是創建一個空文件的劇本
---
- hosts: all
tasks:
- name: create an empty file
file:
path: "/home/test_file.txt"
state: touch
become: yes
當我運行這個命令
$ ansible-playbook -i myhosts -K playbook1.yml
這是 output:
BECOME password:
PLAY [all] *************************************************************************
TASK [Gathering Facts] *************************************************************
fatal: [user1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: david@192.168.56.4: Permission denied (publickey,password).", "unreachable": true}
fatal: [user2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: david@192.168.56.3: Permission denied (publickey,password).", "unreachable": true}
PLAY RECAP *************************************************************************
user1 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
user2 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
請注意這一行:“無法通過 ssh 連接到主機:david@192.168.56.4 和 david@192.168.56.3”。 大衛是我 PC 上的主要用戶,而不是虛擬機上的主要用戶。
它應該是 user1@192.168.56.4 和 user2@192.168.56.3 (這些是我擁有的兩個虛擬機上的用戶名)
我嘗試了什么:我嘗試使用命令
$ ansible-playbook -i myhosts -K playbook1.yml --users user2
它有效,但僅適用於第二個用戶。
我也嘗試復制VM ssh 密鑰並將其粘貼到VM root
cp -R /home/user1/.ssh /root/.ssh
cp -R /home/user2/.ssh /root/.ssh
當我運行這個命令時
$ ansible-playbook -i myhosts -K playbook1.yml --user root
僅適用於一個 VM,而不適用於第二個 VM
$ ansible-playbook -i myhosts -K playbook1.yml --user root
BECOME password:
PLAY [all] *************************************************************************
TASK [Gathering Facts] *************************************************************
fatal: [user1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: root@192.168.56.4: Permission denied (publickey,password).", "unreachable": true}
[DEPRECATION WARNING]: Distribution debian 10.8 on host user2 should use
/usr/bin/python3, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the
discovered platform python for this host. See https://docs.ansible.com/ansible/2.10
/reference_appendices/interpreter_discovery.html for more information. This feature
will be removed in version 2.12. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
ok: [user2]
TASK [create an empty file] ******************************************
changed: [user2]
PLAY RECAP *************************************************************************
user1 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
user2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
僅適用於一個虛擬機,但不適用於第二個虛擬機。
這不是您以清單的ini格式定義用戶的方式。
這一點和特定的用戶用例包含在以下內容中: 將變量分配給一台機器:主機變量,您可以在其中看到必須使用ansible_user=foo
分配用戶
所以你的主機文件應該是:
[servers]
my_ubuntu_server ansible_host=192.168.56.4 ansible_user=user1
my_debian_server ansible_host=192.168.56.3 ansible_user=user2
那里的主機的別名並不重要,只要您指定ansible_host
,您就可以最好地了解 Ansible 當前正在哪個節點上運行。
值得一讀: 庫存別名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.