ansible定时任务模块和用户组模块使用
接上篇,还是一些基础模块的使用,这里主要介绍的是系统模块的使用。
3、用户模块的使用
用户模块主要用来管理用户账号和用户的属性(对远程主机用户进行批量管理)。
用户模块依赖的指令为useradd,userdel,usermod
|
参数 |
必填 |
默认值 |
选项 |
说明 |
|
Append |
否 |
No |
Yes/no |
如果没有指定group,append设定为yes,那么会添加到用户同名组;append设定为no,那么会添加到user组。如果指定了group,那么都会添加在指定的group组 |
|
Comment |
否 |
用户的备注信息 |
||
|
Force |
否 |
No |
Yes/no |
当状态为absent的时候,相当于userdel -force |
|
generate_ssh_key |
否 |
No |
Yes/no |
是否生成秘钥 |
|
Group |
否 |
可选的,设定用户的主组 |
||
|
Groups |
否 |
用逗号分隔的组,当groups设定为空的时候,那么会移除除了主组的其他所有组 |
||
|
Home |
否 |
可选的,设定为用户的home目录 |
||
|
Login_class |
否 |
可选的,设定用户的登录类 FreeBSD, OpenBSD and NetBSD systems. |
||
|
Name |
是 |
用户创建,移除,修改 |
||
|
Move_home |
否 |
No |
Yes/no |
如果使用了选项home=设置为yes,那么会将用户家目录移到不存在的home目录中 |
|
Non_unique |
否 |
No |
Yes/no |
可选的,当使用-u选项的时候,将用户的uid设置为non_unique |
|
Password |
否 |
设定用户的密码 |
||
|
Remove |
否 |
No |
Yes/no |
当使用状态为state=absent的时候,差不多和userdel --remove(删除所有信息) |
|
Shell |
否 |
设定用户的shell |
||
|
Ssh_key_bits |
否 |
2048 |
设定秘钥的位数 |
|
|
Ssh_key_comments |
否 |
¥HOSTHOME |
Ssh key备注信息 |
|
|
Ssh_key_file |
否 |
.sha/id_rsa |
秘钥的文件名 |
|
|
ssh_key_passphrase |
否 |
Ssh秘钥的密码 |
||
|
Ssh_key_type |
否 |
Rsa |
Ssh秘钥的类型 |
|
|
State |
否 |
Present |
Present Absent |
新增删除 |
|
System |
否 |
No |
Yes/no |
创建为系统账号,不会改变已经存在的用户 |
|
Uid |
否 |
设定为用户的uid |
||
|
Update_password |
否 |
Always |
Always On_create |
Always当用户密码不同,会修改,是否需要修改密码 |
3.1 添加用户
|
[root@ansibleserver ~]# ansible pythonserver -m user -a "name=kelly group=kelly uid=555 comment='kelly' state=present" SSH password: 192.168.1.60 | success >> { "changed": true, "comment": "kelly", "createhome": true, "group": 501, "home": "/home/kelly", "name": "kelly", "shell": "/bin/bash", "state": "present", "system": false, "uid": 555 } |
添加用户,指定用户名name,指定用户主组kelly,设定用户uid为555 ,设定备注信息
3.2 添加用户使用append
|
[root@ansibleserver ~]# ansible pythonserver -m user -a "name=kelly shell=/bin/bash groups=kelly,kel append=yes" SSH password: 192.168.1.60 | success >> { "changed": true, "comment": "", "createhome": true, "group": 100, "groups": "kelly,kel", "home": "/home/kelly", "name": "kelly", "shell": "/bin/bash", "state": "present", "system": false, "uid": 501 } |
添加用户,将用户添加在组kelly和kel中,指定shell为/bin/bash,然后将用户添加组user中
3.3 删除用户
|
[root@ansibleserver ~]# ansible pythonserver -m user -a "name=kelly state=absent remove=yes" SSH password: 192.168.1.60 | success >> { "changed": true, "force": false, "name": "kelly", "remove": true, "state": "absent" } |
将用户强制删除
3.4 新建用户创建sshkey
|
[root@ansibleserver ~]# ansible pythonserver -m user -a "name=kelly generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" SSH password: 192.168.1.60 | success >> { "changed": true, "comment": "", "createhome": true, "group": 501, "home": "/home/kelly", "name": "kelly", "shell": "/bin/bash", "ssh_fingerprint": "2048 cd:18:dc:17:3b:ac:d8:ec:95:15:39:26:01:3d:17:d6 /home/kelly/.ssh/id_rsa.pub (RSA)", "ssh_key_file": "/home/kelly/.ssh/id_rsa", "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAs6vB5rR5KAkWGbCmnBFBCLTuD8W3Gu2ehGtlkS9ObCC3uANHG7l80g3tfUl9k3GTh1a+vlAq2XKIKgFSnqQwitiX7WgyL4JzoWHtt0do2JKf0Zh+i7RgR6fZpF48wpuuuZnFXs9WaaHJDhWjp8t7dMDcCI1TxDCCmeYO7bxQdnN8FsxQSQLr5L0IrVBb1avE/+oVCJ72LEmJKGBNo6XQXwC2sA3o0dXEYQwhaE/ulJrPbLbXRJcZmhhIW5Rviu1J3hNOC36+9WpzV7luaZ9QUZdhuvuN3LlXSELWVoHciiGct+6h4zih/aZnmEBCqoD//cobBsuRN6PDJRz+DxRWLw== "state": "present", "system": false, "uid": 501 } |
创建用户,设定sshkey等属性
4、 hostname模块使用
hostname模块主要用来修改主机的名称。
|
参数 |
必填 |
默认 |
选择 |
注释 |
|
Name |
否 |
主机名称 |
4.1 修改主机名称
|
[root@ansibleserver ~]# ansible pythonserver -m hostname -a "name=python" SSH password: 192.168.1.60 | success >> { "changed": true, "name": "python" } |
在查看的时候,主要查看文件/etc/sysconfig/network,重启之后才能生效
5、 ping模块的使用
ping模块主要是无意义的测试模块,主要用来检查ansible是否可以用的模块以及python是否配置好的,在playbook中基本不会使用,在能成功连接之后,总是返回结果pong
|
[root@ansibleserver ~]# ansible all -m ping SSH password: ansiblemoniter | success >> { "changed": false, "ping": "pong" } |
6、定时任务管理模块使用
主要是用来对定时任务进行调度,定时任务模块会包含一句描述信息,格式如下:
|
"#Ansible: <name>" |
name对应的为模块传递过去的参数,主要用来给以后ansible进行操作的时候,查看相关的状态或者检查相关状态
依赖的模块为cron
|
参数 |
必填 |
默认 |
选择 |
说明 |
|
Backup |
非 |
Yes/no |
如果yes,那么在修改之后会进行备份,备份的路径在backup_file |
|
|
Cron_file |
非 |
如果设置了,那么在cron.d中使用此文件替代单独用户的crontab,在使用此选项的时候,必须使用user选项 |
||
|
Day |
非 |
天 |
||
|
Hour |
非 |
小时 ( 0-23, *, */2, etc ) |
||
|
Job |
非 |
需要执行的命令,必须状态为present |
||
|
Minute |
非 |
分 ( 0-59, *, */2, etc ) |
||
|
Month |
非 |
月( 1-12, *, */2, etc ) |
||
|
Name |
非 |
任务的描述 |
||
|
Reboot |
非 |
No |
Yes/no |
重启后是否需要执行 |
|
Special_time |
非 |
reboot yearly annually monthly weekly daily hourly |
特定的执行时间 |
|
|
State |
非 |
Present |
Present Absent |
启用或停用任务 |
|
User |
非 |
Root |
执行任务的用户 |
|
|
Weekday |
非 |
每一周的哪天进行运行(0-6 for Sunday-Saturday, *, etc) |
6.1 新增一个定时任务
|
[root@ansibleserver ~]# ansible pythonserver -m cron -a "name=check minute=5 job='crontab -l >>/root/123'" SSH password: 192.168.1.60 | success >> { "changed": true, "jobs": [ "check" ] } |
新增一个任务,每五分钟执行一次,任务名称为check
6.2 删除定时任务
|
[root@ansibleserver ~]# ansible pythonserver -m cron -a "name=check state=absent" SSH password: 192.168.1.60 | success >> { "changed": true, "jobs": [] } |
删除刚刚新建的定时任务
6.3 新建一个cron文件
|
[root@ansibleserver ~]# ansible pythonserver -m cron -a "name='for test' weekday='2' minute='0' hour=12 user='root' job='cat /etc/passwd >/root/111' cron_file='test ansible'" SSH password: 192.168.1.60 | success >> { "changed": true, "cron_file": "test ansible", "jobs": [ "for test" ] } |
新增一个任务,在目录/etc/cron.d/目录中,文件名称为test ansible,用户为root
6.4 删除一个cron文件
|
[root@ansibleserver ~]# ansible pythonserver -m cron -a "name='for test' cron_file='test ansible' state=absent" SSH password: 192.168.1.60 | success >> { "changed": true, "cron_file": "test ansible", "jobs": [] } |
删除上面新建的cron文件。
7、 setup模块
这个模块在playbook中自动被查找的,从而得到远程主机的相关信息,可以作为变量使用。
|
参数 |
必填 |
默认 |
选择 |
说明 |
|
Fact_path |
否 |
/etc/ansible/facts.d |
Fact的路径 |
|
|
Filter |
否 |
* |
过滤串 |
7.1 收集fact并且进行保存
|
ansible pythonserver -m setup --tree /tmp/facts |
执行之后,会显示相关的fact,并且在/tmp/facts中会保存fact信息,如下:
|
[root@ansibleserver tmp]# ls -l /tmp/facts/ total 12 -rw-r--r-- 1 root root 8990 Jan 18 13:16 192.168.1.60 |
使用--tree选项,在分类的时候,会根据主机的名称进行分类
7.2 收集内存信息并输出
|
[root@ansibleserver tmp]# ansible pythonserver -m setup -a "filter=ansible_*_mb" SSH password: 192.168.1.60 | success >> { "ansible_facts": { "ansible_memfree_mb": 746, "ansible_memtotal_mb": 996, "ansible_swapfree_mb": 2015, "ansible_swaptotal_mb": 2015 }, "changed": false } |
使用过滤字符串,从而进行了相关的匹配,得到相关的内存信息
7.3 收集主机网卡信息
|
[root@ansibleserver tmp]# ansible pythonserver -m setup -a "filter=ansible_eth[02]" SSH password: 192.168.1.60 | success >> { "ansible_facts": { "ansible_eth0": { "active": true, "device": "eth0", "ipv4": { "address": "192.168.1.60", "netmask": "255.255.255.0", "network": "192.168.1.0" }, "ipv6": [ { "address": "fe80::a00:27ff:fee5:e8a8", "prefix": "64", "scope": "link" } ], "macaddress": "08:00:27:e5:e8:a8", "module": "e1000", "mtu": 1500, "promisc": false, "type": "ether" } }, "changed": false } |
收集特定的网卡信息
ansible定时任务模块和用户组模块使用的更多相关文章
- Ansible安装部署以及常用模块详解
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- ansible环境部署及常用模块总结 - 运维笔记
一. Ansible 介绍Ansible是一个配置管理系统configuration management system, python 语言是运维人员必须会的语言, ansible 是一个基于py ...
- Ansible安装部署及常用模块详解
Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...
- ansible学习基础知识和模块(一)
基础知识补充: 常用自动化运维工具 Ansible:使用python来开发的,无需设置Agentless(代理),一般管理几百台.与ssh的方式也不一样,ssh是基于c/s模式(客户端+服务器)来使用 ...
- ansible服务模块和组模块使用
本篇文章主要是介绍ansible服务模块和组模块的使用. 主要模块为ansible service module和ansible group moudle,下面的内容均是通过实践得到,可以直接运行相关 ...
- CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机
使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...
- ansible执行shell模块和command模块报错| FAILED | rc=127 >> /bin/sh: lsof: command not found和| rc=2 >> [Errno 2] No such file or directory
命令: ansible -i hosts_20 st -m shell -a 'service zabbix_agentd star' -K --become ansible -i hosts_2 ...
- Ansible基础配置与常用模块使用
环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113 一.创建Ansibl ...
- Ansible 开发调试 之【模块调试】
本地调试 需要安装jinja2 库 yum -y install python-jinja2 使用官方提供的测试脚本调试 git clone git://github.com/ansible/ansi ...
随机推荐
- UITextField的总结
1.UITextField的初始化和设置 textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- Eclipse —— java文件修改后不能及时体现
有没有遇到过这么一种情况:某一个对象某一属性,先设值为 1 .打印出来是 1.再设值为 2 .打印出来仍是 1 ? …… 原来是工具Eclipse设置的问题. 你检查一下,Eclipse 工具栏 - ...
- [原]poj-2680-Choose the best route-dijkstra(基础最短路)
题目大意: 已知n 个点,m条路线,s为终点:给出m条路线及其权值:给出w个起点,求最短路! 思路:基础的dijkstra,有向无环正权最短路,只要把终点和起点 reverse考虑便可. AC代码如下 ...
- YTU 2601: 熟悉题型——填空题(删除线性表节点)
2601: 熟悉题型--填空题(删除线性表节点) 时间限制: 1 Sec 内存限制: 128 MB 提交: 357 解决: 212 题目描述 给出一串具体长度的数据,删除指定数据. 已经给出部分代 ...
- BZOJ 1018 堵塞的交通traffic(线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1018 题意:一个2*n的格子,相邻格子之间有一条道路.初始时道路是不通的. 三种操作:( ...
- oracle创建表空间、创建用户、授权、夺权、删除用户、删除表空间
表空间定义 表空间是为了统一ORACLE物理和逻辑上的结构而专门建立的,从物理上来说,一个表空间是由具体的一个或多个磁盘上数据文件构成的(至少1对1,可以1对多),从逻辑上来说一个表空间是由具体的一个 ...
- 运行时报错 ADB server didn’t ACK
查看进程中所有和ADB有关的进程,全都结束了,包括什么豌豆荚之类的(大多数情况是占用端口),之后重新启动Eclipse.
- 点滴积累【JS】---JS小功能(createElement和insertBefore添加div下面的节点)
效果: 代码: <head runat="server"> <title></title> <script type="text ...
- R2的版本由来
给人一杯水,自己先有一桶水.上课.备课,那么备课中就常有一些稀奇古怪的问题. 学生问:SP2和R2是一样的吗? 老师答:不一样,一个是补丁程序,另一个是服务器操作系统. 学生不解:R2如果是操作系统, ...