管理Ansible配置文件

  • 1.配置Ansible
  • 2.配置文件的优先级
  • 3管理配置文件的设置
  • 4.配置连接
  • 4.1清单位置
  • 4.2连接设置
  • 4.3升级特权
  • 4.4非ssh连接
  • 4.5配置文件注释
  • 5.模块的使用
  • 6.在被管理节点上创建、修改、删除用户


1.配置Ansible

可以通过修改 Ansible 配置文件中的设置来自定义 Ansible安装的行为。Ansible从控制节点上多个可能的位置之一选择其配置文件。

使用/etc/ansible/ansible.cfg
ansible软件包提供一个基本的配置文件,它位于/etc/ansible/ansible.cfg。如果找不到其他配置文件,则使用此文件

使用./ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。

推荐的做法是在需要运行Ansible命令的目录中创建ansible.cfg文件。此目录中也将包含任何供Ansible项目使用的文件,如清单和playbook。这是用于Ansible配置文件的最常用位置。实践中不常使用~/.ansible.cfg或/etc/ansible/ansible.cfg文件

使用ANSIBLE_CONFIG环境变量

我们可以通过将不同的配置文件放在不同的目录中,然后从适当的目录执行Ansible命令,以此利用配置文件。但是,随着配置文件数量的增加,这种方法存在局限性并且难以管理。有一个更加灵活的选项,即通过ANSIBLE_CONFIG环境变量定义配置文件的位置。定义了此变量时,Ansible将使用变量所指定的配置文件,而不用上面提到的任何配置文件。

2.配置文件的优先级

ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该变量,则接下来检查运行ansible命令的目录中是否有ansible.cfg文件。如果不存在该文件,则检查用户的家目录是否有.ansible.cfg文件。只有在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件。如果/etc/ansible/ansible.cfg配置文件不存在,Ansible包含它使用的默认值。

Ansible配置文件使用优先级
ANSIBLE_CONFIG > 当前目录下的ansible.cfg > ~/.ansible.cfg > /etc/ansible/ansible.cfg

  • 如果家目录里面没有其它任何的ansible配置文件,它默认用官方的配置文件
[root@master ~]# pwd
/root
[root@master ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg    //这一行可以看到是使用的是默认的官方配置文件
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]#
  • 现在在家目录里面创建一个ansible配置文件
[root@master ~]# touch ansible.cfg
[root@master ~]# ansible --version
ansible 2.9.23
  config file = /root/ansible.cfg    //这一行可以看到它优先使用的是家目录的配置文件,因为家目录的优先级是高于默认配置文件的
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]#
  • 如果是在当前目录下那么它会优先使用当前目录下的配置文件
[root@master project]# pwd
/opt/project
[root@master project]# ansible --version
ansible 2.9.23
  config file = /opt/project/ansible.cfg    
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master project]#
  • 这里可以看到在家目录下它优先使用anaconda-ks.cfg配置文件
[root@master ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg  initial-setup-ks.cfg
模板  图片  下载  桌面  ansible.cfg
[root@master ~]# ansible --version
ansible 2.9.23
  config file = /root/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]# 

[root@master ~]# ansible --version
ansible 2.9.23
  config file = /root/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]#
  • 我们可以做一个环境变量可以优先使用默认配置文件
[root@master ~]# export ANSIBLE_CONFIG=/etc/ansible/ansible.cfg 
[root@master ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master ~]# 

//进入到当前目录下发现优先使用的还是环境变量设置的默认配置文件
[root@master ~]# cd /opt/project/
[root@master project]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master project]# 

//现在取消环境变量
[root@master project]# export ANSIBLE_CONFIG=
[root@master project]# ansible --version
ansible 2.9.23
  config file = /opt/project/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
[root@master project]# 

由于Ansible配置文件可以放入的位置有多种,因此Ansible当前使用哪一个配置文件可能会令人困惑。我们可以运行以下命令来清楚地确认所安装的Ansible版本以及正在使用的配置文件。
ansible --version

Ansible仅使用具有最高优先级的配置文件中的设置。即使存在优先级较低的其他配置文件,其设置也会被忽略,不会与选定配置文件中的设置结合。因此,如果你选择自行创建配置文件来取代全局/etc/ansible/ansible.cfg配置文件,就需要将该文件中所有需要的设置复制到自己的用户级配置文件中。用户组配置文件中未定义的设置将保持设为内置默认值,即使已在全局配置文件中设为某个其他值也是如此。

3管理配置文件的设置

Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分的标题以中括号括起来。对于基本操作,请使用以下两部分:

  • [defaults]部分设置Ansible操作的默认值
  • [privilege_escalation]配置Ansible如何在受管主机上执行特权升级

例如,下面是典型的ansible.cfg文件:

[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

下表说明了此文件中的指令:
Ansible配置

指令

描述

inventory

指定清单文件的路径。

remote_user

要在受管主机上登录的用户名。如果未指定则使用当前用户名

ask_pass

是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false

become

连接后是否自动在受管主机上切换用户(通常切换为root)这也可以通过play来指定。

become_method

如何切换用户(通常为sudo,这也是默认设置,但可选择su)

become_user

要在受管主机上切换到的用户(通常是root,这也是默认值)

become_ask_pass

是否需要为become_method提示输入密码。默认为false。

4.配置连接

Ansible需要知道如何与其受管主机通信。更改配置文件的一个最常见原因是为了控制Ansible使用什么方法和用户来管理受管主机。需要的一些信息包括:

  • 列出受管主机和主机组的清单的位置
  • 要使用哪一种连接协议来与受管主机通信(默认为SSH),以及是否需要非标准网络端口来连接服务器
  • 要在受管主机上使用哪一远程用户;这可以是root用户或者某一非特权用户
  • 如果远程用户为非特权用户,Ansible需要知道它是否应尝试将特权升级为root以及如何进行升级(例如,通过sudo)
  • 是否提示输入SSH密码或sudo密码以进行登录或获取特权

4.1清单位置

在[defaults]部分中,inventory指令可以直接指向某一静态清单文件,或者指向含有多个静态清单文件和动态清单脚本的某一目录。

[defaults]
inventory = ./inventory     //./inventory 和inventory都是指定当前目录
[root@master project]# ls
ansible.cfg  inventory
[root@master project]# mv inventory i1     //现在将inventory文件重命名为i1
[root@master project]# mkdir inventory    //创建一个inventory目录
[root@master project]# ls
ansible.cfg  i1  inventory
[root@master project]# mv i1 inventory/   //将i1文件移动到inventory目录中去
[root@master project]# cd inventory/
[root@master inventory]# ls
i1
[root@master inventory]# cat i1 
[webservers]
192.168.8.130
[root@master inventory]# vim i2     //创建一个不存在的主机也放在inventory目录中
[root@master inventory]# 
1.1.1.1
[root@master inventory]# cat i*
[webservers]
192.168.8.130
1.1.1.1

[root@master inventory]# 
[root@master project]# vim ansible.cfg 

# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

#inventory      = /etc/ansible/hosts
inventory      = inventory    //这一行可以看到它现在指定的是invenory目录,它里面包含了刚刚创建的i1和i2主机文件
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
  • 验证效果
[root@master project]# ansible all -m ping
192.168.8.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
1.1.1.1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 1.1.1.1 port 22: Connection timed out",
    "unreachable": true
}
[root@master project]#

4.2连接设置

默认情况下,Ansible使用SSH协议连接受管主机。控制Ansible如何连接受管主机的最重要参数在[defaults]部分中设置。

默认情况下,Ansible尝试连接受管主机时使用的用户名与运行ansible命令的本地用户相同。若要指定不同的远程用户,请将remote_user参数设置为该用户名。

如果为运行Ansible的本地用户配置了SSH私钥,使得它们能够在受管主机上进行远程用户的身份验证,则Ansible将自动登录。如果不是这种情况,可以通过设置指令ask_pass = true,将Ansible配置为提示本地用户输入由远程用户使用的密码。

[defaults]
inventory = ./inventory

remote_user = root
ask_pass = true
  • 在节点一创建一个tom用户在管理主机用tom身份登录
[root@node1 ~]# id tom
id: “tom”:无此用户
[root@node1 ~]# useradd tom 
[root@node1 ~]# echo "redhat" | passwd --stdin tom
更改用户 tom 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@node1 ~]# 

[root@master project]# ls
ansible.cfg  inventory
[root@master project]# vim ansible.cfg 

# config file for ansible -- https://ansible.com/

# change this for alternative sudo implementations
#sudo_exe = sudo

# What flags to pass to sudo
# WARNING: leaving out the defaults might create unexpected behaviours
#sudo_flags = -H -S -n

# SSH timeout
#timeout = 10

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
remote_user = tom    //在管理机上将root用户改为tom用户

[root@master project]# ls
ansible.cfg  inventory
[root@master project]# vim inventory 

[webservers]
192.168.8.130 ansible_password=redhat    //在这里指定密码,用户默认已经指定为tom
  • 验证效果
[root@master project]# ansible all -m ping
192.168.8.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@master project]# 

//回到节点一切换到tom用户,可以看到家目录是没有任何文件和目录
[root@node1 ~]# su - tom
[tom@node1 ~]$ ls
[tom@node1 ~]$ 

[root@master project]# ansible all -m command -a 'touch abc'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
192.168.8.130 | CHANGED | rc=0 >>

[root@master project]# 

[tom@node1 ~]$ pwd
/home/tom
[tom@node1 ~]$ ls
abc
[tom@node1 ~]$ 
[tom@node1 ~]$ ll
总用量 0
-rw-rw-r--. 1 tom tom 0 7月  15 10:14 abc
[tom@node1 ~]$

假设在使用一个Linux控制节点,并对受管主机使用OpenSSH,如果可以使用密码以远程用户身份登录,那么我们可以设置基于SSH密钥的身份验证,从而能够设置ask_pass = false

第一步是确保在~/.ssh中为控制节点上的用户配置了SSH密钥对。并且使用ssh-copy-id命令将本地的公钥复制到受管主机中。此过程请参考文章01Ansible介绍与安装

4.3升级特权

鉴于安全性和审计原因,Ansible可能需要先以非特权用户身份连接远程主机,然后再通过特权升级获得root用户身份的管理权限。这可以在Ansible配置文件的[privilege_escalation]部分中设置。

要默认启用特权升级,可以在配置文件中设置指令become = true。即使默认为该设置,也可以在运行临时命令或Ansible Playbook时通过各种方式覆盖它。(例如,有时候可能要运行一些不需要特权升级的任务或play。)

become_method指令指定如何升级特权。有多个选项可用,但默认为使用sudo。类似地,become_user指令指定要升级到的用户,但默认为root。

如果所选的become_method机制要求用户输入密码才能升级特权,可以在配置文件中设置become_ask_pass = true指令。

以下示例ansible.cfg文件假设你可以通过基于SSH密钥的身份验证以someuser用户身份连接受管主机,并且someuser可以使用sudo以root用户身份运行命令而不必输入密码:

[defaults]
inventory = ./inventory
remote_user = someuser
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
示例:

[root@master project]# vim ansible.cfg 


# ignore these extensions when parsing a directory as inventory source
#ignore_extensions = .pyc, .pyo, .swp, .bak, ~, .rpm, .md, .txt, ~, .orig, .ini, .cfg, .retry

# ignore files matching these patterns when parsing a directory as inventory source
#ignore_patterns=

# If 'true' unparsed inventory sources become fatal errors, they are warnings otherwise.
#unparsed_is_failed=False

[privilege_escalation]      //将这一模块内注释给去掉,并加一行sudo的密码
become=True
become_method=sudo
become_user=root
become_pass=redhat
#become_ask_pass=False

[paramiko_connection]

[root@master project]# cat inventory    //清单文件中也加入sudo的密码
[webservers]
192.168.8.130 ansible_password=redhat ansible_sudo_pass=redhat 
[root@master project]# 

//回到节点一打开visudo
root@node1 ~]# visudo

# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
tom    ALL=(ALL)       ALL       //在这一行加上tom
  • 验证效果
[root@master project]# ansible all -m ping
192.168.8.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@master project]# 

[root@master project]# ansible all -m command -a 'touch pyd'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
192.168.8.130 | CHANGED | rc=0 >>

[root@master project]#

//返回节点一发现创建文件的属主属组都是root
[tom@node1 ~]$ ls
abc
[tom@node1 ~]$ ll
总用量 0
-rw-rw-r--. 1 tom tom 0 7月  15 10:33 abc
[tom@node1 ~]$ ll
总用量 0
-rw-rw-r--. 1 tom  tom  0 7月  15 10:33 abc
-rw-r--r--. 1 root root 0 7月  15 10:48 pyd
[tom@node1 ~]$

4.4非ssh连接

默认情况下,Ansible用于连接受管主机的协议设置为smart,它会确定使用SHH的最高效方式。可以通过多种方式将其设置为其他的值。

例如,默认使用SSH的规则有一个例外。如果目录中没有localhost,Ansible将设置一个隐式localhost条目以便允许运行以localhost为目标的临时命令和playbook。这一特殊清单条目不包括在all或ungrouped主机组中。此外,Ansible不使用smart SSH连接类型,而是利用默认的特殊local连接类型来进行连接。
local连接类型忽略remote_user设置,并且直接在本地系统上运行命令。如果使用特权升级,它会在运行sudo时使用运行Ansible命令的用户,而不是remote_user。如果这两个用户具有不同的sudo特权,这可能会导致混淆。

如果你要确保像其他受管主机一样使用SSH连接localhost,一种方法是在清单中列出它。但是,这会将它包含在all和ungrouped组中,而你可能不希望如此。

另一种方法是更改用于连接localhost的协议。执行此操作的最好方法是为localhost设置ansible_connection主机变量。为此,你需要在运行Ansible命令的目录中创建host_vars子目录。在该子目录中,创建名为localhost的文件,其应含有ansible_connection:smart这一行。这将确保对localhost使用smart(SSH)连接协议,而非local。

你也可以通过另一种变通办法来使用它。如果清单中列有127.0.0.1,则默认情况下,将会使用smart来连接它。也可以创建一个含有ansible_connection: local这一行的host_vars/127.0.0.1件,它会改为使用local。

ansible localhost --list-hosts

4.5配置文件注释

Ansible配置文件允许使用两种注释字符:井号或分号。

位于行开头的#号会注释掉整行。它不能和指令位于同一行中。

分号字符可以注释掉所在行中其右侧的所有内容。它可以和指令位于同一行中,只要该指令在其左侧。

5.模块的使用

  • 列出本机上所有的模块
[root@master project]# ansible-doc -l   
a10_server                                                    Manage A10 >
a10_server_axapi3                                             Manage A10 >
a10_service_group                                             Manage A10 >
a10_virtual_server                                            Manage A10 >
aci_aaa_user                                                  Manage AAA >
aci_aaa_user_certificate                                      Manage AAA >
aci_access_port_block_to_access_port                          Manage port>
aci_access_port_to_interface_policy_leaf_profile              Manage Fabr>
aci_access_sub_port_block_to_access_port                      Manage sub >
aci_aep                                                       Manage atta>
aci_aep_to_domain                                             Bind AEPs t>
aci_ap                                                        Manage top >
aci_bd                                                        Manage Brid>
aci_bd_subnet                                                 Manage Subn>
aci_bd_to_l3out                                               Bind Bridge>
  • 查看某个模块的帮助文档 ansible-doc MOD_NAME
[root@master ~]# ansible-doc  ping    //查看ping模块的帮助文档
> PING    (/usr/lib/python3.6/site-packages/ansible/modules/system/ping.p>

        A trivial test module, this module always returns `pong' on
        successful contact. It does not make sense in playbooks, but
        it is useful from `/usr/bin/ansible' to verify the ability to
        login and that a usable Python is configured. This is NOT ICMP
        ping, this is just a trivial test module that requires Python
        on the remote-node. For Windows targets, use the [win_ping]
        module instead. For Network targets, use the [net_ping] module
        instead.

  * This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):

- data
        Data to return for the `ping' return value.
        If this parameter is set to `crash', the module will cause an
        exception.
        [Default: pong]
        type: str


SEE ALSO:
      * Module net_ping
           The official documentation on the net_ping module.
           https://docs.ansible.com/ansible/2.9/modules/net_ping
        _module.html
      * Module win_ping
           The official documentation on the win_ping module.
           https://docs.ansible.com/ansible/2.9/modules/win_ping
        _module.html


AUTHOR: Ansible Core Team, Michael DeHaan
        METADATA:
          status:
          - stableinterface
          supported_by: core
        

EXAMPLES:

# Test we can logon to 'webservers' and execute python with json lib.
# ansible webservers -m ping

# Example from an Ansible Playbook
- ping:

# Induce an exception to see what happens
- ping:
    data: crash


RETURN VALUES:

ping:
    description: value provided with the data parameter
    returned: success
    type: str
    sample: pong
  • 过滤用户模块
[root@master ~]# ansible-doc -l | grep user
aci_aaa_user                                                  Manage AAA us...
aci_aaa_user_certificate                                      Manage AAA us...
avi_cloudconnectoruser                                        Module for se...
avi_user                                                      Avi User Modu...
avi_useraccount                                               Avi UserAccou...
avi_useraccountprofile                                        Module for se...
bigip_apm_acl                                                 Manage user-d...
bigip_remote_user

6.在被管理节点上创建、修改、删除用户

  • 用管理主机在节点上创建一个pyd的用户,并指定uid为2021
[root@master project]# ansible all -m user -a 'name=pyd uid=2021 state=present'    //-m 指定用户模块 -a指定参数为pyd 
192.168.8.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 2021,
    "home": "/home/pyd",
    "name": "pyd",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 2021
}
[root@master project]# 

[root@node1 ~]# id pyd
uid=2021(pyd) gid=2021(pyd) 组=2021(pyd)
[root@node1 ~]#
  • 修改pyd的uid为2025
[root@master project]# ansible all -m user -a 'name=pyd uid=2025'
192.168.8.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 2021,
    "home": "/home/pyd",
    "move_home": false,
    "name": "pyd",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 2025
}
[root@master project]# 

[root@node1 ~]# id pyd 
uid=2025(pyd) gid=2021(pyd) 组=2021(pyd)
[root@node1 ~]#
  • 删除pyd这个用户
[root@master project]# ansible all -m user -a 'name=pyd state=absent remove=yes'
192.168.8.130 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "pyd",
    "remove": true,
    "state": "absent"
}
[root@master project]# 

[root@node1 ~]# id pyd
id: “pyd”:无此用户
[root@node1 ~]#