Ansible学习笔记8

group模块:

创建一个group组:

bash 复制代码
[root@localhost ~]# ansible group1 -m group -a "name=aaa gid=5000"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 5000,
    "name": "aaa",
    "state": "present",
    "system": false
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 5000,
    "name": "aaa",
    "state": "present",
    "system": false
}

检查group组的创建:

bash 复制代码
[root@agent1 tmp]# grep aaa /etc/group
aaa:x:5000:

默认创建用户,并将bbb添加到group5000这个组中。

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=bbb group=5000"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 5000,
    "home": "/home/bbb",
    "name": "bbb",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 5000,
    "home": "/home/bbb",
    "name": "bbb",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1000
}

检查:

bash 复制代码
[root@agent1 tmp]# grep bbb /etc/passwd
bbb:x:1000:5000::/home/bbb:/bin/bash

把用户添加到一系列组中。

bash 复制代码
[root@agent1 tmp]# id bbb
uid=1000(bbb) gid=5000(aaa) groups=5000(aaa)

只有先删除bbb用户,然后再删除组:

bash 复制代码
[root@localhost ~]# ansible group1 -m user -a "name=bbb state=absent"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "bbb",
    "remove": false,
    "state": "absent"
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "bbb",
    "remove": false,
    "state": "absent"
}
[root@localhost ~]#
[root@localhost ~]# ansible group1 -m group -a "name=aaa state=absent"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "aaa",
    "state": "absent"
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "aaa",
    "state": "absent"
}

操作的过程中,还要注意删除用户的家目录。

cron模块:

cron模块,用于管理周期性时间任务。

创建一个cron任务,不指定user的话,默认是root(因为我这里是用root操作的)。

如果minute、hour、day、month、week不指定的话,默认是*。

对周期任务比较熟悉的话,那这个模块还是很好理解的。

vim /etc/crontab:

注意:其中要配置用户名,再写要干的事情。

如果我们直接使用crontab -e,就不用写用户,因为就是使用root用户来操作的。

然后还需要记住一个路径:/var/spool/cron 这个路径:

创建一个案例:

bash 复制代码
[root@localhost ~]# ansible group1 -m cron -a "name='cron1' user=root job='touch /tmp/222' minute=*/2 hour=1,3-5"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "cron1"
    ]
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "cron1"
    ]
}
bash 复制代码
[root@agent1 tmp]# crontab -l
#Ansible: cron1
*/2 1,3-5 * * * touch /tmp/222

删除cron:

bash 复制代码
[root@localhost ~]# ansible group1 -m cron -a "name='cron1' state=absent"
192.168.17.105 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
192.168.17.106 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}
bash 复制代码
[root@agent1 tmp]# crontab -l
[root@agent1 tmp]#

通过文件的方式也可以进行操作:

这个思路需要掌握下。

相关推荐
꧁༺朝花夕逝༻꧂1 天前
Ansible小试牛刀
ansible
꧁༺朝花夕逝༻꧂1 天前
Ansible+Zabbix-agent2快速实现对多主机监控
运维·ansible
m0_628017592 天前
项目四.高可用集群_ansible
ansible
云攀登者-望正茂2 天前
通过 Ansible 在 Windows 2022 上安装 IIS Web 服务器
ansible
云攀登者-望正茂5 天前
使用 Ansible 在 Windows 服务器上安装 SSL 证书
ansible
tianyuanwo6 天前
Ansible自动化运维全解析:从设计哲学到实战演进
运维·自动化·ansible
哆啦A梦15886 天前
在golang中如何将已安装的依赖降级处理,比如:将 go-ansible/[email protected] 更换为 go-ansible/@v1.1.7
开发语言·golang·ansible
lifeng43217 天前
在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
centos·github·ansible
运维成长记12 天前
ansible-playbook 进阶 接上一章内容
linux·服务器·ansible
半桶水专家12 天前
Ansible 配置Playbook文件格式、关键字和语法详解
ansible