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
相关推荐
独隅3 天前
Cursor+GitOps 自动化运维实战:智能编写 Ansible 与 Terraform
运维·自动化·ansible
酷可达拉斯6 天前
自动化运维-ansible配置文件与主机清单
linux·运维·自动化·ansible
杜子不疼.6 天前
Ansible定时清空日志实战:Playbook、Cron与远程主机接入
ansible
布鲁飞丝7 天前
vivo Pulsar 万亿级消息处理实践()-Ansible运维部署
运维·ansible
爱网络爱Linux10 天前
RHCSA+RHCE 10.0全新升级!红帽Ansible自动化运维
运维·自动化·ansible·rhce认证·rhel1o·linux10·rhce10
码上上班15 天前
Ansible课程
ansible
至乐活着18 天前
Ansible自动化运维实战:从零部署高可用Nginx+静态网站集群
ansible·自动化运维·devops·playbook·配置管理
John Song21 天前
ansible-playbook常用的检查命令
windows·ansible
明航咨询_贾老师23 天前
RHCA Ansible高级自动化(DO374)认证信息整理
运维·自动化·ansible·rhca·红帽认证
有毒的教程1 个月前
Ansible批量操作自动化完整教程:批量部署服务、配置同步、软件更新实战
运维·自动化·ansible