• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

实例学习ansible系列(8)常用模块之user/group

Ansible 蜷缩的蜗牛 8个月前 (01-23) 16次浏览 已收录

知识点:user 模块,用于管理用户。
知识点:group 模块,用于管理 group。

使用 user 模块添加用户

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a "id test01"</span>
host32 | FAILED | rc=<span class="hljs-number">1</span> >>
<span class="hljs-attribute">id</span>: <span class="hljs-attribute">test01</span>: <span class="hljs-literal">no</span> such user

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m user -a "name=test01 group=root"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"comment"</span>: <span class="hljs-string">""</span>,
    <span class="hljs-string">"createhome"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"group"</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">"home"</span>: <span class="hljs-string">"/home/test01"</span>,
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"test01"</span>,
    <span class="hljs-string">"shell"</span>: <span class="hljs-string">"/bin/bash"</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"present"</span>,
    <span class="hljs-string">"system"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">"uid"</span>: <span class="hljs-number">1001</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a "id test01"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
uid=<span class="hljs-number">1001</span>(test01) gid=<span class="hljs-number">0</span>(root) groups=<span class="hljs-number">0</span>(root)

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

使用 user 模块删除用户

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m user -a "name=test01 state=absent remove=yes"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"force"</span>: <span class="hljs-literal">false</span>,
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"test01"</span>,
    <span class="hljs-string">"remove"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"absent"</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a "id test01"</span>
host32 | FAILED | rc=<span class="hljs-number">1</span> >>
<span class="hljs-attribute">id</span>: <span class="hljs-attribute">test01</span>: <span class="hljs-literal">no</span> such user

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

使用 group 模块添加 group

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "cat /etc/group |grep testgrp01"</span>
host32 | FAILED | rc=<span class="hljs-number">1</span> >>


[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m group -a "name=testgrp01"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"gid"</span>: <span class="hljs-number">1001</span>,
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"testgrp01"</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"present"</span>,
    <span class="hljs-string">"system"</span>: <span class="hljs-literal">false</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "cat /etc/group |grep testgrp01"</span>
host32 | SUCCESS | rc=<span class="hljs-number">0</span> >>
<span class="hljs-attribute">testgrp01</span>:<span class="hljs-attribute">x</span>:<span class="hljs-number">1001</span>:

[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

使用 group 模块删除 group

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m group -a "name=testgrp01 state=absent"</span>
host32 | SUCCESS<span class="hljs-function"> =></span> {
    <span class="hljs-string">"changed"</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-string">"name"</span>: <span class="hljs-string">"testgrp01"</span>,
    <span class="hljs-string">"state"</span>: <span class="hljs-string">"absent"</span>
}
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "cat /etc/group |grep testgrp01"</span>
host32 | FAILED | rc=<span class="hljs-number">1</span> >>


[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

本文转载自 实例学习 ansible 系列(8)常用模块之 user/group


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(8)常用模块之 user/group
喜欢 (0)
[]
分享 (0)