ansible使用普通用户sudo切换为root方式记录

1、首先客户端创建普通用户,记得保留用户的家目录,并设置该用户的密码

 

2、客户端修改sudoer文件添加sudo用户 并设置切换不提示密码,可执行所有权限,如下;

 

用户名  ALL=(root:root) NOPASSWD: ALL

 

3、设置该用户在控制机器上的免密登录,可以使用

ssh-copy-id命令直接进行公钥的复制,前提是第一步已经为该用户设置了密码。

 

[root@netsa ~]# ssh-copy-id 普通用户@主机地址
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
zhangbei@114.115.141.68's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'zhangbei@114.115.141.68'"
and check to make sure that only the key(s) you wanted were added.

此时会拷贝公钥内容到客户端机器的家目录的.ssh目录下的authorized_keys文件。

 

4、测试免密登录

 

5、修改控制机的ansible 主机清单,在对应主机后面添加如下参数:

客户机ip或者主机名  ansible_user=普通用户 ansible_become=yes ansible_become_user=root ansible_become_method=sudo 

6、测试命令

ansible 主机组  -m ping  是否正常返回 pong

posted @ 2023-02-24 10:57  忙碌在路上  阅读(533)  评论(0编辑  收藏  举报