Ansible:ansible 的 SSH 错误

     2023-03-23     117

关键词:

【中文标题】Ansible:ansible 的 SSH 错误【英文标题】:Ansible: SSH Error with ansible 【发布时间】:2016-09-14 11:03:57 【问题描述】:

我正在主机 A 上部署堆栈,我想在主机 B 上执行配置后脚本。这些脚本应该从 ansible 控制器(主机 C)或从主机 A 推送到主机 B。

为了实现这一点,我创建了一个测试剧本。

- hosts: all
  tasks:
    - name: Executin python script
      shell: /usr/bin/python test.py
#      when: "'10.17.10.85' in groups['monserver']"
      when: "'monserver' in  group_names "
      register: test
    - debug: test.stdout

我正在尝试使用“何时”的方法并得到相同的错误。

库存文件有两个主机,都被隔离在不同的组中。

[controller]
10.17.10.150    ansible_ssh_user=hyroot

[monservers]
10.17.10.85     ansible_ssh_user=ubuntu

我在运行 playbook 时遇到错误,同时我可以手动 ssh 无密码主机。

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
<10.17.10.150> ESTABLISH CONNECTION FOR USER: hyroot
<10.17.10.85> ESTABLISH CONNECTION FOR USER: ubuntu
<10.17.10.150> REMOTE_MODULE setup
<10.17.10.85> REMOTE_MODULE setup
<10.17.10.150> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=hyroot', '-o', 'ConnectTimeout=10', '10.17.10.150', "/bin/sh -c 'mkdir -p /tmp/ansible-tmp-1463545052.75-6584687306251 && chmod a+rx /tmp/ansible-tmp-1463545052.75-6584687306251 && echo /tmp/ansible-tmp-1463545052.75-6584687306251'"]
<10.17.10.85> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ubuntu', '-o', 'ConnectTimeout=10', '10.17.10.85', "/bin/sh -c 'mkdir -p /tmp/ansible-tmp-1463545052.75-48027114165698 && chmod a+rx /tmp/ansible-tmp-1463545052.75-48027114165698 && echo /tmp/ansible-tmp-1463545052.75-48027114165698'"]
<10.17.10.150> PUT /tmp/tmpfmKMrv TO /tmp/ansible-tmp-1463545052.75-6584687306251/setup
<10.17.10.150> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=hyroot', '-o', 'ConnectTimeout=10', '10.17.10.150', "/bin/sh -c 'chmod a+r /tmp/ansible-tmp-1463545052.75-6584687306251/setup'"]
<10.17.10.150> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=hyroot', '-o', 'ConnectTimeout=10', '10.17.10.150', u"/bin/sh -c 'LANG=C LC_CTYPE=C /usr/bin/python /tmp/ansible-tmp-1463545052.75-6584687306251/setup; rm -rf /tmp/ansible-tmp-1463545052.75-6584687306251/ >/dev/null 2>&1'"]
<10.17.10.85> PUT /tmp/tmpYPmXeY TO /tmp/ansible-tmp-1463545052.75-48027114165698/setup
<10.17.10.85> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ubuntu', '-o', 'ConnectTimeout=10', '10.17.10.85', "/bin/sh -c 'chmod a+r /tmp/ansible-tmp-1463545052.75-48027114165698/setup'"]
<10.17.10.85> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ubuntu', '-o', 'ConnectTimeout=10', '10.17.10.85', u'/bin/sh -c \'su hyroot -k && su hyroot -H -S -p "[sudo via ansible, key=tponxjlokfvzaswnynqyxdejohcambhm] password: " -u hyroot /bin/sh -c \'"\'"\'echo SUDO-SUCCESS-tponxjlokfvzaswnynqyxdejohcambhm; LANG=C LC_CTYPE=C /usr/bin/python /tmp/ansible-tmp-1463545052.75-48027114165698/setup; rm -rf /tmp/ansible-tmp-1463545052.75-48027114165698/ >/dev/null 2>&1\'"\'"\'\'']
<10.17.10.85> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=ubuntu', '-o', 'ConnectTimeout=10', '10.17.10.85', "/bin/sh -c 'rm -rf /tmp/ansible-tmp-1463545052.75-48027114165698/ >/dev/null 2>&1'"]
failed: [10.17.10.85] => "failed": true, "parsed": false
invalid output was: su: invalid option -- 'k'

Usage:
 su [options] [-] [USER [arg]...]

Change the effective user id and group id to that of USER.
A mere - implies -l.   If USER not given, assume root.

Options:
 -m, -p, --preserve-environment  do not reset environment variables
 -g, --group <group>             specify the primary group
 -G, --supp-group <group>        specify a supplemental group

 -, -l, --login                  make the shell a login shell
 -c, --command <command>         pass a single command to the shell with -c
 --session-command <command>     pass a single command to the shell with -c
                                 and do not create a new session
 -f, --fast                      pass -f to the shell (for csh or tcsh)
 -s, --shell <shell>             run shell if /etc/shells allows it

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see su(1).
OpenSSH_6.7p1 Debian-5, OpenSSL 1.0.1k 8 Jan 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 29670
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 1
Shared connection to 10.17.10.85 closed.

<10.17.10.150> EXEC ['ssh', '-C', '-tt', '-vvv', '-o', 'ControlMaster=auto', '-o', 'ControlPersist=60s', '-o', 'ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r', '-o', 'KbdInteractiveAuthentication=no', '-o', 'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', '-o', 'PasswordAuthentication=no', '-o', 'User=hyroot', '-o', 'ConnectTimeout=10', '10.17.10.150', "/bin/sh -c 'rm -rf /tmp/ansible-tmp-1463545052.75-6584687306251/ >/dev/null 2>&1'"]
ok: [10.17.10.150]

TASK: [Executin python script] ************************************************
skipping: [10.17.10.150]

TASK: [debug icinga_stdout] ***************************************************
<10.17.10.150> ESTABLISH CONNECTION FOR USER: hyroot
ok: [10.17.10.150] => 
    "msg": "Hello world!"


PLAY RECAP ********************************************************************
           to retry, use: --limit @/root/pytask.retry

10.17.10.150               : ok=2    changed=0    unreachable=0    failed=0
10.17.10.85                : ok=0    changed=0    unreachable=0    failed=1

我还在日志中注意到 ansible 跳过是在 10.17.10.150 上执行 python 脚本。

我希望 ansible 在 10.17.10.85 上执行 python 脚本, 而不是 10.17.10.150。

【问题讨论】:

【参考方案1】:

我找到了答案..! sudo_exe = su myuser 已在 ansible.cfg 文件中启用!

【讨论】:

ansible使用密码登录

参考技术A安装ansible1.安装sshpassapt:源码:创建一个hosts文件,添加用户密码,认证ssh连接2.错误汇总:ansible-ihoststest-mping3.原因和解决办法:ssh第一次连接的时候一般会提示输入yes进行确认为将key字符串加入到~/.ssh/known_hosts文件中... 查看详情

ansible内置参数

参考技术Ainventory内置参数参考解释例子1)ansible_ssh_host将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.eg:ansible_ssh_host=192.169.1.1232)ansible_ssh_port  ssh端口号.如果不是默认的端口号,通过此变量设... 查看详情

ansible内置主机变量与魔法变量

一、内置主机变量所谓内置变量其实就是ansible.cfg配置文件中的选项,在其前加上ansible_即成为内置变量。当然内置变量拥有比ansible.cfg中选项更高的优先级,而且针对不同的主机,可以定义不同的值。之所以叫内置主机变量,其... 查看详情

006.ansible自定义变量(代码片段)

ansible支持变量,用于存储会在整个项目中重复使用到的一些值。以简化项目的创建与维护,降低出错的机率。变量的定义:变量名应该由字母、数字下划数组成变量名必须以字母开头ansible内置关键字不能作为变量名1.在Inventory... 查看详情

ansible的优化

Ansible企业实战环境中,如果管理的服务器越来越多,Ansibe执行效率会变得比较慢,可以通过优化Ansible提供工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ansible执行变得缓慢,也需要对Openssh进行优化,具体优化... 查看详情

ansible批量推送ssh私钥(代码片段)

1ansible主机生成ssh密钥对ssh-keygen然后全部回车即可 2指定推送ssh公钥的主机vi/etc/ansible/hosts[push]主机IP1ansible_ssh_user="用户"ansible_ssh_pass="密码"ansible_ssh_port=22主机IP2ansible_ssh_user="用户"ansible_ssh_pass="密码"ansibl 查看详情

ansible安装配置(代码片段)

CentOS7.5一,安装yum-yinstallansible 二,配置hosts文件/etc/ansible/hostss1ansible_ssh_port=65522ansible_ssh_host=10.0.0.1ansible_ssh_user=simon 常用参数配置:ansible_ssh_host#目标主机地址ansible_ssh_port#目标主机端口,默认22ansible_ssh_user#目标主机用... 查看详情

ansible+ssh自动化运维

章节介绍一、ansible是个什么东西呢?二、ansible有如下优点:三、ansible安装四、ansibl语法格式:五、ansible帮助模块:六、ansible参数详细介绍七、ANSIBLE剧本(PLAYBOOKS)功能 一、ansible是个什么东西呢?官方的title是“AnsibleisSim... 查看详情

ansible常用模块实例(代码片段)

ansible主要文件(/etc/ansible/ansible.cfg)#ansible主配置文件。(/etc/ansible/hosts)#主机清单,保存管理的主机信息。(/etc/ansible/roles)#公共角色,主要在自动化部署多台主机时应用。 ansible命令集ansible定义并运行简单任务。ansible-... 查看详情

Ansible:如何配置没有 ssh 密钥的跳转主机?

】Ansible:如何配置没有ssh密钥的跳转主机?【英文标题】:Ansible:HowdoIconfigureajumphostwithoutsshkeys?【发布时间】:2017-11-0116:27:29【问题描述】:我查看了[tofaq][1],但我的任务没有答案。如何通过Ansible使用密码访问堡垒(跳转框)... 查看详情

ansible批量更新远程主机用户密码(包括ansible批量做ssh互信)(代码片段)

1) 在服务端安装ansible1[root@ansible-server~]#yuminstall-yansible2)配置ansible到远程主机的ssh无密码信任关系(authoried_keys 模块)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374... 查看详情

ansible-hoc命令行

ansible一种开源的自动化工具ansible: hoc命令行: 是一款开源的自动化运维工具 python paramiko #模拟ssh协议批量管理主机 jinja2 #模板语言,主要用来传递变量 yaml #相当于是一种编程语言 控制方式: 1. 免密钥:key-gen 参数形式:... 查看详情

Ansible - 禁用 ssh 密码验证

】Ansible-禁用ssh密码验证【英文标题】:Ansible-disablesshpasswordauthentication【发布时间】:2021-03-2112:03:07【问题描述】:我正在尝试制作正确处理sshd_config的ansible任务。我从其他问题中找到了类似的正则表达式,但它们什么也没做。... 查看详情

《ansible权威指南》笔记——安装,ssh密钥登陆,命令

2016-12-23读这本《Ansible权威指南》学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记。另,这本书内容很好,但印刷错误比较多,作者说第二版会改进,还没买的小伙伴们可以买第二版。 一、安... 查看详情

ansible介绍

Ansible是一个同时管理多个远程主机的软件(任何可以通过SSH协议登录的机器),因此Ansible以管理远程虚拟机、物理机、也可以是本地主机(Linux、Windows)。Ansible通过SSH协议实现管理节点,远程节点的通信。只要是能够SSH登录的... 查看详情

ansible(代码片段)

一、安装ansibleyuminstallansible-yansible--version//查看版本,没有报错即安装成功二、ansible主机定义与分组1.ansible配置文件查找顺序(意思是以前边配置文件为准,前边有后边就不生效了)首先检测ANSIBLE_CONFIG变量定义的配置文件其次检... 查看详情

ansible不配ssh连接,用户密码登录(代码片段)

1yuminstall-yansible23wgethttps://nchc.dl.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz4tar-xzfsshpass-1.06.tar.gz5cdsshpass-1.066./configure7make8makeinstall910#主配置文件/etc/ansible/ansible.cfg11#受控主机清单/etc/ansible/hosts12vim/etc/ansible/hosts13[common]14[api]1... 查看详情

ansible常用模块

执行前设定它的Config文件呢,默认在/etc/ansible/ansible.cfg第一步,就是我们的ansible的机器,需要链接上被它控制的机器。因为ansible的ssh是默认有个检查key的设置,我们第一次使用它,肯定对面机器没有Publickey啊,所以我们要关闭... 查看详情