Vagrant ansible:即使将“忽略错误”设置为“忽略”,playbook仍然会失败;是”;

Vagrant ansible:即使将“忽略错误”设置为“忽略”,playbook仍然会失败;是”;,vagrant,ansible,Vagrant,Ansible,我在ansible剧本中得到了以下任务 - name: Check if default ssh user exist shell: whoami remote_user: "{{default_ssh_user_info.name}}" ignore_errors: yes register: results 目标是检查远程计算机上是否配置了用户{{default\u ssh\u user\u info.name},如果不是,我将以根用户身份连接并使用以下

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

- name: Check if default ssh user exist  
  shell: whoami  
  remote_user: "{{default_ssh_user_info.name}}"  
  ignore_errors: yes  
  register: results  
目标是检查远程计算机上是否配置了用户
{{default\u ssh\u user\u info.name}
,如果不是,我将以根用户身份连接并使用以下任务创建它

但是,
ignore\u errors
似乎没有捕获错误。。。这是我唯一的剧本站。有没有办法抓住这种错误?是虫子吗

以下是错误消息:

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不会直接在您的任务中失败,而是在连接到主机时已经失败了。这是一个致命错误,您不能忽略此级别的错误

我知道你有三个选择

  • 以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
    
但我想知道你为什么一开始就需要这样一张支票。当您使用Ansible时,它只会在用户不存在的情况下创建用户(呃…它怎么可能不存在;-)。如果您想根据用户是否刚创建而触发自己的操作,则可以注册该输出并检查
更改的
属性

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

- debug: msg="User was created"
  when: user_created | changed
请包括错误和命令执行结果。