通过 Shell 命令在远程 GCP 服务器上运行 Ansible

通过 Shell 命令在远程 GCP 服务器上运行 Ansible

我有 3 台 Linux 服务器 A、B 和 C。A 是所有开发人员使用的地方,B 是具有 playbook 的 Ansible 服务器,C 是我运行某些任务所需的目标服务器。

这是剧本的框架 - play.yml:

---
- name: get inst info
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:

    - debug: msg="Before going to {{ target_instance }}"

- hosts: "{{ target_instance }}"
  become: yes
  become_user: root

  tasks:

  - debug: msg="In {{ target_instance }}"

在 A 上,我将发出命令:

user_a@A~$ ssh B "cd <ansible_playbook_dir>; ansible-playbook play.yml -e \"target_instance=wolf\""

和输出:

PLAY [get inst info] ********************************************************************************************************************************************************************************************************************

TASK [Involve general variables] ********************************************************************************************************************************************************************************************************
Thursday 04 November 2021  23:08:00 +0000 (0:00:00.023)       0:00:00.023 ***** 
ok: [localhost]

TASK [Involve prodcopy variables] *******************************************************************************************************************************************************************************************************
Thursday 04 November 2021  23:08:00 +0000 (0:00:00.021)       0:00:00.045 ***** 
ok: [localhost]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Thursday 04 November 2021  23:08:01 +0000 (0:00:00.019)       0:00:00.065 ***** 
ok: [localhost] => {
    "msg": "Before going to wolf"
}

PLAY [wolf] *************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
Thursday 04 November 2021  23:08:01 +0000 (0:00:00.077)       0:00:00.143 ***** 
ok: [wolf]

TASK [debug] ****************************************************************************************************************************************************************************************************************************
Thursday 04 November 2021  23:08:02 +0000 (0:00:01.877)       0:00:02.021 ***** 
ok: [wolf] => {
    "msg": "In wolf"
}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
wolf                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Thursday 04 November 2021  23:08:02 +0000 (0:00:00.031)       0:00:02.052 ***** 
=============================================================================== 

但是,如果我与具有 ssh 密钥密码的用户运行相同的命令,

user_b@A~$ ssh B "cd <ansible_playbook_dir; ansible-playbook play.yml -e \"target_instance=wolf\""

我收到 ssh 错误:

PLAY [get inst info] ***********************************************************

TASK [Involve general variables] ***********************************************
Thursday 04 November 2021  23:00:42 +0000 (0:00:00.025)       0:00:00.025 ***** 
ok: [localhost]

TASK [Involve prodcopy variables] **********************************************
Thursday 04 November 2021  23:00:42 +0000 (0:00:00.039)       0:00:00.065 ***** 
ok: [localhost]

TASK [debug] *******************************************************************
Thursday 04 November 2021  23:00:42 +0000 (0:00:00.037)       0:00:00.103 ***** 
ok: [localhost] => {
    "msg": "Before going to wolf"
}

PLAY [wolf] ****************************************************************

TASK [Gathering Facts] *********************************************************
Thursday 04 November 2021  23:00:42 +0000 (0:00:00.048)       0:00:00.151 ***** 
fatal: [wolf]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: user_b@wolf: Permission denied (publickey).", "unreachable": true}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
wolf                       : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

Thursday 04 November 2021  23:00:42 +0000 (0:00:00.066)       0:00:00.218 ***** 
=============================================================================== 

我在 A 和 B 上为 user_b 设置了钥匙串。

无论如何,我可以让拥有 ssh 密钥密码的用户使用此功能吗?

谢谢!

相关内容