ansible报错解决python依赖,Ansible普通用户执行命令

问题

报错1

执行ansible报错python这一个

[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host k8s-node01 should use /usr/bin/python3, but is using /usr/bin/python for backward 
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in
version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

 

image-20210729113427170

 

报错2

 

"ansible_facts": {
      "discovered_interpreter_python": "/usr/bin/python"
  },

image-20210729113638171

 

 

解决

 

从上面可知基本都是意思远程机器调用的python2,建议python3,看机器也是有的。

root@k8s-node01:/home/appdeploy# ls /usr/bin/python
python     python2     python2.7   python3     python3.5   python3.5m python3m

image-20210729113859878

 

那么在ansible的host内调用即可

 

k8s-node01 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
k8s-node02 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5

image-20210729114003463

 

修改后

image-20210729140541021

 

 

ansible普通用户执行命令

 

生产中root都是禁止直接远程登录得。很多命令root权限才能执行,脚本使用expect提权又麻烦,还好ansible自带有这功能。

修改下列配置文件

root@k8s-master01:~# cat /etc/ansible/ansible.cfg 
[defaults]
timeout = 30
remote_user = root
[inventory]
[privilege_escalation]
become=True           ###表示打开become开关,也就是输入密码那一栏
become_method=su     ###表示用什么方式将普通账户切换到root或所需的其他账户,这里可以用su或sudo
become_user=root           ##***设置为root账户,相当于我们以普通账户登入到远程主机时,再使用su - root切换为root账户。
become_ask_pass=True   ###表示询问密码
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

 

修改后执行root权限得命令会弹出让你输入BECOME密码(也就是你远程主机root的密码),然后执行成功。

 

image-20210810142045819

 

 

附上其他两个配置文件参考:

ansible主机配置文件

image-20210810142634304

root@k8s-master01:~# cat /etc/ansible/hosts  |grep -Ev '^$|#'
[k8s]
k8s-node01 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
k8s-node02 ansible_ssh_user=appdeploy ansible_ssh_port=51000 ansible_python_interpreter=/usr/bin/python3.5
root@k8s-master01:~# fengliuxiaosanyuanchuang

 

系统主机hosts文件

image-20210810142715952

 

root@k8s-master01:~# cat /etc/hosts  |grep -Ev '^$|#'
127.0.0.1 localhost
127.0.1.1 ubuntu
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.202.128 k8s-master01
192.168.202.132 k8s-node01
192.168.202.130 k8s-node02

 

posted @ 2021-08-10 14:40  疯刘小三  阅读(2822)  评论(0编辑  收藏  举报