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
相关推荐
yourkin6662 天前
Ansible
ansible
Ribou2 天前
ansible 自动安装软件,实现列表选择软件的方法
ansible
文静小土豆2 天前
Ansible 自动化部署K8S1.34.1
kubernetes·自动化·ansible
K_i1344 天前
Helm 与 Ansible 深度对比解析文档
自动化·ansible
zmjjdank1ng5 天前
什么是Ansible 清单
服务器·自动化·ansible
Yyyy4825 天前
ansible role配apt源
ansible
K_i13411 天前
Ansible实战:VMware下K8s自动化部署指南
kubernetes·自动化·ansible
许泽宇的技术分享11 天前
Ansible核心架构深度剖析:从源码看IT自动化的“简单“哲学
python·ansible·自动化运维·devops·it基础设施
荣光波比11 天前
Ansible(三)—— 使用Ansible自动化部署LNMP环境实战指南
运维·自动化·云计算·ansible
tt666qq12 天前
运维自动化之 Ansible 核心知识点总结
运维·自动化·ansible