Loading

ansible 连接错误

ansible 连接错误

[root@CentOS7.9]# ansible -i ans del -m shell -a "df -h"
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ValueError: Key name may not begin with an underscore
200.200.8.67 | FAILED! => {
"changed": false, 
"module_stderr": "Shared connection to 200.200.8.67 closed.\r\n", 
"module_stdout": "Traceback (most recent call last):\r\n File \"/home/zabbix/.ansible/tmp/ansible-tmp-1617009135.16-47503-91402564498682/AnsiballZ_command.py\", line 102, in <module>\r\n _ansiballz_main()\r\n File \"/home/zabbix/.ansible/tmp/ansible-tmp-1617009135.16-47503-91402564498682/AnsiballZ_command.py\", line 94, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/home/zabbix/.ansible/tmp/ansible-tmp-1617009135.16-47503-91402564498682/AnsiballZ_command.py\", line 40, in invoke_module\r\n runpy.run_module(mod_name='ansible.modules.commands.command', init_globals=None, run_name='__main__', alter_sys=True)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 176, in run_module\r\n fname, loader, pkg_name)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 82, in _run_module_code\r\n mod_name, mod_fname, mod_loader, pkg_name)\r\n File \"/usr/lib64/python2.7/runpy.py\", line 72, in _run_code\r\n exec code in run_globals\r\n File \"/tmp/ansible_command_payload_w9iIL2/ansible_command_payload.zip/ansible/modules/commands/command.py\", line 344, in <module>\r\n File \"/tmp/ansible_command_payload_w9iIL2/ansible_command_payload.zip/ansible/modules/commands/command.py\", line 238, in main\r\n File \"/tmp/ansible_command_payload_w9iIL2/ansible_command_payload.zip/ansible/module_utils/basic.py\", line 784, in __init__\r\n File \"/tmp/ansible_command_payload_w9iIL2/ansible_command_payload.zip/ansible/module_utils/basic.py\", line 2062, in _log_invocation\r\n File \"/tmp/ansible_command_payload_w9iIL2/ansible_command_payload.zip/ansible/module_utils/basic.py\", line 2021, in log\r\n File \"systemd/_journal.pyx\", line 68, in systemd._journal.send\r\n File \"systemd/_journal.pyx\", line 32, in systemd._journal._send\r\nValueError: Key name may not begin with an underscore\r\n", 
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", 
"rc": 1
}

这种错误可能是目标主机上安装了两个版本的python,python2 和python3

这里有两个可用的办法:

1. -e添加变量#

ansible_python_interpreter这个变量设置了使用哪个版本的python。

}
[root@CentOS7.9]# ansible -i ans del -m shell -a "df -h"  -e ansible_python_interpreter=/usr/bin/python3
200.200.8.67 | CHANGED | rc=0 >>
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               3.9G     0  3.9G    0% /dev
tmpfs                  3.9G     0  3.9G    0% /dev/shm
tmpfs                  3.9G  402M  3.5G   11% /run
tmpfs                  3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  3.6G   13G   23% /
/dev/sda1              473M  158M  315M   34% /boot
/dev/mapper/rhel-home   32G  4.9G   26G   17% /home
/dev/mapper/rhel-tmp   7.3G   19M  6.9G    1% /tmp
/dev/mapper/rhel-var   9.1G  2.8G  5.9G   32% /var
tmpfs                  783M     0  783M    0% /run/user/1003
tmpfs                  783M     0  783M    0% /run/user/891
tmpfs                  783M     0  783M    0% /run/user/1000
tmpfs                  783M     0  783M    0% /run/user/900
tmpfs                  783M     0  783M    0% /run/user/1004

但是缺点也很明显,每次都要加上这个特别麻烦,来看第二个方法,一步到位。

2. 把变量添加到hosts#

找到hosts列表编辑

vim ans 在ip地址后加上 ansible_python_interpreter=/usr/bin/python3 保存退出。

200.200.8.67 ansible_python_interpreter=/usr/bin/python3

这次可以了

[root@CentOS7.9]# ansible -i ans del -m shell -a "df -h" 
200.200.8.67 | CHANGED | rc=0 >>
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               3.9G     0  3.9G    0% /dev
tmpfs                  3.9G     0  3.9G    0% /dev/shm
tmpfs                  3.9G  402M  3.5G   11% /run
tmpfs                  3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  3.6G   13G   23% /
/dev/sda1              473M  158M  315M   34% /boot
/dev/mapper/rhel-home   32G  4.9G   26G   17% /home
/dev/mapper/rhel-tmp   7.3G   19M  6.9G    1% /tmp
/dev/mapper/rhel-var   9.1G  2.8G  5.9G   32% /var
tmpfs                  783M     0  783M    0% /run/user/1003
tmpfs                  783M     0  783M    0% /run/user/891
tmpfs                  783M     0  783M    0% /run/user/1000
tmpfs                  783M     0  783M    0% /run/user/900
tmpfs                  783M     0  783M    0% /run/user/1004
posted @ 2021-03-29 17:32  五月的麦田  阅读(1293)  评论(0编辑  收藏  举报