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
相关推荐
道清茗6 天前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》常见问题
ansible
何中应7 天前
Ansible安装&使用
运维·自动化·ansible·运维开发·自动化运维
王九思7 天前
Ansible 自动化运维基础—模板
运维·自动化·ansible
道清茗8 天前
【RH294知识点汇总】第 7 章 《 使用角色和 Ansible 内容集合简化 Playbook 》
java·前端·ansible
切糕师学AI8 天前
深入解析 Ansible:从入门到实践
ansible
王的宝库8 天前
【Ansible】变量与敏感数据管理:Vault 加密 + Facts 采集详解
笔记·学习·ansible
张32310 天前
Ansible拆分大型Playbook
linux·ansible
爱莉希雅&&&10 天前
Ansible+Docker案例(含ansible配置安装docker)
linux·运维·mysql·nginx·docker·容器·ansible
张32310 天前
配置加载混淆(vars_files与from_yaml)
ansible
张32310 天前
角色与内容集合:自动化配置的标准化复用机制
运维·自动化·ansible