ansible定时任务模块和用户组模块使用
2016-01-18 21:35
701 查看
接上篇,还是一些基础模块的使用,这里主要介绍的是系统模块的使用。
下面例子都进行过相关的实践,从而可以直接进行使用相关的命令。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 } |
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 } |
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== ansible-generated", "state": "present", "system": false, "uid": 501 } |
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" } |
5、 ping模块的使用
ping模块主要是无意义的测试模块,主要用来检查ansible是否可以用的模块以及python是否配置好的,在playbook中基本不会使用,在能成功连接之后,总是返回结果pong[root@ansibleserver ~]# ansible all -m ping SSH password: ansiblemoniter | success >> { "changed": false, "ping": "pong" } |
6、定时任务管理模块使用
主要是用来对定时任务进行调度,定时任务模块会包含一句描述信息,格式如下:"#Ansible: <name>" |
依赖的模块为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" ] } |
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" ] } |
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": [] } |
7、 setup模块
这个模块在playbook中自动被查找的,从而得到远程主机的相关信息,可以作为变量使用。参数 | 必填 | 默认 | 选择 | 说明 |
Fact_path | 否 | /etc/ansible/facts.d | Fact的路径 | |
Filter | 否 | * | 过滤串 |
7.1 收集fact并且进行保存
ansible pythonserver -m setup --tree /tmp/facts |
[root@ansibleserver tmp]# ls -l /tmp/facts/ total 12 -rw-r--r-- 1 root root 8990 Jan 18 13:16 192.168.1.60 |
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 } |
相关文章推荐
- Python 中的range()函数与array()函数
- ansible定时任务模块和用户组模块使用
- 计算机刊物SCI影响因子排名
- 游戏保护 游戏安全 反外挂系统 MMProtect发布
- MySQL自定义查询字段排序
- 一起talk C栗子吧(第一百零六回:C语言实例--生产者与消费者问题二)
- OpenLayers学习笔记5——使用jQuery UI实现查询并标注(UI篇)
- python小知识
- CentOS Linux因别名问题引起的麻烦及解决技巧
- Spring概述
- illustrator将图片转换成ai路径
- bat脚本中 %errorlevel%的解释
- Java中利用ZXing生成二维码
- IOS真机调试:swift工程不能在ios8真机上运行问题解决
- opengl 教程(24) shadow mapping (2)
- 使用SQLMAP对网站和数据库进行SQL注入攻击
- kuangbin_ShortPath R (HDU 4370)
- Two Sum 的map函数
- 变量和基本类型
- openSceneGraphic 删除多个子节点