gpt4 book ai didi

vagrant - 可靠的 : playbook still fails even if ignore_errors is set to "yes"

转载 作者:行者123 更新时间:2023-12-04 18:04:56 25 4
gpt4 key购买 nike

我在 ansible 剧本中得到了以下任务...

- name: Check if default ssh user exist  
shell: whoami
remote_user: "{{default_ssh_user_info.name}}"
ignore_errors: yes
register: results

目标是检查远程机器上是否配置了用户 {{default_ssh_user_info.name}},如果没有,我会以 root 用户身份连接并使用以下任务。

但是,看起来 ignore_errors 没有捕捉到错误……我的 ansible 剧本就此停止。有没有办法捕获这种错误?是错误吗?

错误信息如下:

fatal: [local.datadevserver.aplazame.com] => SSH encountered an unknown error. The output was:
OpenSSH_6.6.1, OpenSSL 1.0.1f 6 Jan 2014
debug1: Reading configuration data /home/updiversity/.ssh/config
debug1: /home/updiversity/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/home/updiversity/.ansible/cp/ansible-ssh-192.168.33.10-22-cox" does not exist
debug2: ssh_connect: needpriv 0
debug1: Connecting to 192.168.33.10 [192.168.33.10] port 22.
debug2: fd 3 setting O_NONBLOCK
debug1: fd 3 clearing O_NONBLOCK
debug1: Connection established.
debug3: timeout: 10000 ms remain after connect
debug3: Incorrect RSA1 identifier
debug3: Could not load "/home/updiversity/.ssh/id_rsa" as a RSA1 public key
debug1: identity file /home/updiversity/.ssh/id_rsa type 1
debug1: identity file /home/updiversity/.ssh/id_rsa-cert type -1
debug1: identity file /home/updiversity/.ssh/id_dsa type -1
debug1: identity file /home/updiversity/.ssh/id_dsa-cert type -1
debug1: identity file /home/updiversity/.ssh/id_ecdsa type -1
debug1: identity file /home/updiversity/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/updiversity/.ssh/id_ed25519 type -1
debug1: identity file /home/updiversity/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 pat OpenSSH_6.6.1* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "192.168.33.10" from file "/home/updiversity/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/updiversity/.ssh/known_hosts:3
debug3: load_hostkeys: loaded 1 keys
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-dss-cert-v00@openssh.com,ssh-ed25519,ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: zlib@openssh.com,zlib,none
debug2: kex_parse_kexinit: zlib@openssh.com,zlib,none
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-ripemd160-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-md5-96-etm@openssh.com,hmac-md5,hmac-sha1,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_setup: setup hmac-md5-etm@openssh.com
debug1: kex: server->client aes128-ctr hmac-md5-etm@openssh.com zlib@openssh.com
debug2: mac_setup: setup hmac-md5-etm@openssh.com
debug1: kex: client->server aes128-ctr hmac-md5-etm@openssh.com zlib@openssh.com
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA

a7:6c:80:f6:a8:00:ba:df:5e:2a:57:f5:49:f1:63:da
debug3: load_hostkeys: loading entries for host "192.168.33.10" from file "/home/updiversity/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/updiversity/.ssh/known_hosts:3
debug3: load_hostkeys: loaded 1 keys
debug1: Host '192.168.33.10' is known and matches the ECDSA host key.
debug1: Found key in /home/updiversity/.ssh/known_hosts:3
debug1: ssh_ecdsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /home/updiversity/.ssh/id_rsa (0x7f23bb7b3ca0),
debug2: key: updiversity@PC-Lenovo (0x7f23bb7b94f0),
debug2: key: /home/updiversity/.ssh/id_dsa ((nil)),
debug2: key: /home/updiversity/.ssh/id_ecdsa ((nil)),
debug2: key: /home/updiversity/.ssh/id_ed25519 ((nil)),
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred gssapi-with-mic,gssapi-keyex,hostbased,publickey
debug3: authmethod_lookup publickey
debug3: remaining preferred: ,gssapi-keyex,hostbased,publickey
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/updiversity/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: updiversity@PC-Lenovo
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/updiversity/.ssh/id_dsa
debug3: no such identity: /home/updiversity/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/updiversity/.ssh/id_ecdsa
debug3: no such identity: /home/updiversity/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/updiversity/.ssh/id_ed25519
debug3: no such identity: /home/updiversity/.ssh/id_ed25519: No such file or directory
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey,password).



FATAL: all hosts have already failed -- aborting

跟进

我正在尝试以下解决方案:

  - name: Check if default ssh user exist
shell: id -u {{ default_ssh_user_info.name }}
remote_user: "{{ init_ssh_user_info.name }}"
ignore_errors: yes
register: results
sudo: yes
when: init_ssh_user_info.name is defined

我遇到的唯一问题是 SSH 连接仍然失败,因为它找不到私钥。是否可以为 Ansible 中的特定任务指定私钥的路径?我在文档中找不到它。

最佳答案

这里的问题是,Ansible 不会直接在您的任务上失败,而是在连接到主机时已经失败。这是一个 fatal error ,您不能在此级别上ignore_errors

我看到了三个选项,

  • 以 root 身份运行任务(不一定,如果您确定存在另一个用户,您可以使用它)并检查该用户是否存在:

    - shell: id -u {{ default_ssh_user_info.name }}
    register: results
    sudo: yes
  • 通过 shell 任务创建您自己的 ssh 连接:

    - shell: ssh {{ default_ssh_user_info.name }}@{{ inventory_hostname }} whoami
    delegate: localhost
    register: results

但我想知道为什么您首先需要这样的检查。当您使用 user module Ansible 只会在用户不存在时创建用户(呃……怎么可能不存在 ;-))。如果您想根据用户是否刚刚创建来触发自己的操作,您可以注册该输出并检查 changed 属性。

- user: name={{ default_ssh_user_info.name }}
sudo: yes
register: user_created

- debug: msg="User was created"
when: user_created | changed

关于vagrant - 可靠的 : playbook still fails even if ignore_errors is set to "yes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com