ansible 执行命令 FAILED失败

背景

生产工作需要,新增了服务器,加入到了ansible的hosts文件,使用ad-hoc命令执行测试连接异常。

环境说明

项目版本备注
Centos7.9.2009 (Core)被控端系统
Ubuntu18.04主控端系统
ansible2.9.27工具

vi /etc/ansible/hosts

[ops]
10.6.6.161  ansible_ssh_user=[user_name] ansible_ssh_pass=[xxxxxxxx] ansible_become_method=sudo ansible_sudo_pass=[xxxxxxxx]

排查步骤

  1. 使用的普通用户连接连接错误异常,
10.6.6.161 | FAILED! => {======================================================
    "ansible_facts": {   
        "discovered_interpreter_python": "/usr/bin/python"    
    },                   
    "changed": false,      
    "msg": "Invalid/incorrect password: Permission denied, please try again.",   
    "unreachable": true        
} 
  1. 切换到管理员操作命令还是错误异常。

修改hosts文件

[ops]
10.6.6.161  ansible_ssh_user=root ansible_ssh_pass=[xxxxxxxx] 
10.6.6.161 | FAILED! => {======================================================
    "ansible_facts": {   
        "discovered_interpreter_python": "/usr/bin/python"    
    },                   
    "changed": false,      
    "module_stderr": "Shared connection to 10.6.6.161 closed.\r\n",       
    "module_stdout": "/bin/sh: sudo: 未找到命令\r\n",           
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",        
    "rc": 127        
} 
  1. 登录到远程电脑上操作
sudo mkdir /data
bash-: no sudo model

定位

服务器没有sudo模块

处理步骤

安装sudo

yum install sudo -y

验证

重新执行ansible命令,用户为普通用户。

ansible ops -m ping 
j@TK:~$ ansible ops -m ping
10.6.6.161 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

异常-sudo: 需要密码

登录阿里云服务器使用过程中的报错

ssh-copy-id -i /home/username/.ssh/ansible.pub username@192.168.1.114

提示输入分发端服务器的shalter账号密码

输入完毕后,在分发端的

#/home/username/.ssh/生成authorized_keys文件

并且在文件中存储了主控端的rsa值,具备以下数据后,从主控端可以免密登陆分发端主机。

vi /etc/ansible/hosts

[ali]
XXX.XX.XX.XX ansible_ssh_user=username

主控端输入

ansible ali -m ping

弹出如下异常

xx.xxx.xxx.xxx | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "module_stderr": "Shared connection to xx.xxx.xxx.xx closed.\r\n", 
    "module_stdout": "sudo: 需要密码\r\n", 
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
    "rc": 1
}

登录被控端服务器

#visudo

root			ALL=(ALL)		ALL
username	ALL=(ALL)		NOPASSWD:	ALL

:wq

保存退出

重新执行

$  ansible ali -m ping
xx.xxx.xxx.xx | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值