ansible用户管理模块和剧本

ansible用户管理模块和剧本

group创建组模块

user创建⽤户模块

user模块

user模块
name ⽤户名
uid 指定uid
group 指定⽤户组
shell 指定命令解释器
create_home 是否创建家⽬录(yes/no)
state present 添加 absent删除

案例1:创建用户zhangsan

bash 复制代码
ansible web -m user -a 'name=zhangsan'
ansible web -a 'id zhangsan'

案例2:创建用户lisi,指定uid为10010,命令解释器为/sbin/nologin,不创建家目录

bash 复制代码
ansible web -m user -a 'name=lisi uid=10010 shell=/sbin/nologin create_home=no state=present'
ansible web -a 'grep lisi /etc/passwd'

剧本

剧本的格式如下:yaml格式

yaml 复制代码
---
- hosts: all  ---------》 被管理的主机
  vars:    ----> 变量
   filename: test.txt
  tasks:           --------》具体要执行的任务
   - name: touch file    -----> 任务描述
     shell: touch /tmp/{{ filename }} -----》对应模块

案例1:在所有机器的 /opt 目录下创建 application.log文件

yaml 复制代码
[root@m01 scripts]# cat 01-hello.yaml 
---
- hosts: all
  tasks:
    - name: 在/opt目录下创建application.log文件
      shell: touch /opt/application.log
      
ansible all -a 'ls -l /opt'

案例二:添加定时同步时间的定时任务

bash 复制代码
---
- hosts: all
  tasks:
    - name: 添加定时任务
      cron:
        name: "同步时间"
        minute: "*/2"
        job: "/sbin/ntpdate ntp1.aliyun.com &>/dev/null"
        state: present
相关推荐
~莫子21 小时前
Ansible详解+环境准备+主机清单+ansible变量与模块
云原生·ansible
陈皮糖..3 天前
Ansible实战教程----使用Ansible角色源码编译部署nginx服务
linux·运维·nginx·自动化·云计算·ansible
陈皮糖..3 天前
Ansible实战教程----使用Ansible角色自动化部署HTTPD服务
linux·运维·自动化·云计算·ansible
奋斗的蛋黄4 天前
ansible.builtin vs ansible.posix 核心对比
ansible
寂静or沉默5 天前
Ansible 学习总结
学习·ansible
袁煦丞 cpolar内网穿透实验室7 天前
ansible批量自动化清理服务器日志+远程管理!cpolar 内网穿透实验室第 710 个成功挑战
服务器·自动化·ansible·远程工作·内网穿透·cpolar
人间打气筒(Ada)7 天前
ansible之role角色
运维·ansible·自动化运维·template·role·红帽·批量部署
酷酷的崽7987 天前
Ansible解锁便捷运维新方式,内网 NAS 也能远程管
运维·服务器·ansible
WJ.Polar7 天前
Ansible Ad-Hoc命令
linux·运维·网络·ansible