zoukankan      html  css  js  c++  java
  • ansible2.9.5使用become参数实现sudo功能

    一,为什么要使用sudo?

    1,

    生产环境中,为了安全因素,我们不会直接使用root来登录到server,

    确实有需要的情况下,我们再使用sudo切换到root权限。

    所以很多ansible的演示直接使用root还是不够安全的,

    生产环境中的机器也不会开放权限允许root直接登录

    2,

    ansible在新版本中已经不支持sudo参数,

    统一改为使用become方式来实现角色的切换

    继续使用sudo参数会报错如下:

    ansible: error: unrecognized arguments: --sudo

    通过查询手册可以发现,2.9.5中不存在 sudo这个参数:

    [liuhongdi@centos8 ~]$ man ansible

    我们可以通过become方法实现sudo功能

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/

     说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,使用sudo的前提:

    在hosts中配置的登录账号在受控端机器上需要有sudo的权限:

    有疑问可以参见这一篇:

    https://www.cnblogs.com/architectforest/p/12509041.html

    我们在这里的例子中,

    登录到服务器的webop账号,有sudo的权限

    三,ansible使用become的例子:

    1,在ansible配置文件中增加sudo的密码:

    [root@centos8 ~]# vi /etc/ansible/hosts 

    内容为:

    #ansible_ssh_user   登录用户

    #ansible_sudo_pass  登录用户sudo要用的密码(就是登录用户自己的密码)

    [yujian]
    121.122.123.47:12888 ansible_ssh_user=webop  ansible_sudo_pass="weboppass"

    2,实际使用become实现sudo 

    become 激活权限提升

    become_user  您要成为的用户

    become_method 切换新用户的方法:包括:

                     sudo / su / pbrun / pfexec / doas / dzdo / ksu / runas / machinectl

    [liuhongdi@centos8 ~]$  ansible yujian -m shell -a "cd /data/site/think_www;git status;whoami" --become  --become-method=sudo --become-user=root
    121.122.123.47 | CHANGED | rc=0 >>
    On branch master
    Your branch is up to date with 'origin/master'.
    nothing to commit, working tree clean
    root

    四,查看ansible的版本

    [liuhongdi@centos8 ~]$ ansible --version
    ansible 2.9.5

    五,查看centos的版本

    [liuhongdi@centos8 ~]$ cat /etc/redhat-release 
    CentOS Linux release 8.1.1911 (Core)
  • 相关阅读:
    iOS.UIKit.02.UIButton_UILabel
    iOS.UIKit.01.UIView
    如何下载Android源码(window和Linux)
    在Android的c/c++代码中使用LOG
    Android LOG机制流程图
    PowerManager源码
    Android电源管理简介(下)
    Android电源管理简介(上)
    PowerManager使用实例1(下)
    PowerManager使用实例1(上)
  • 原文地址:https://www.cnblogs.com/architectforest/p/12733077.html
Copyright © 2011-2022 走看看