zoukankan      html  css  js  c++  java
  • 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== ansible-generated",

        "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

    }

    收集特定的网卡信息

    for linux and python
  • 相关阅读:
    Linux使用手册
    Oracle&SQL使用记录
    docker的使用
    springboot与mybatis
    JavaScript与TypeScript总结
    React总结
    React与jsplumb
    DB2入门
    吾尝终日而思矣——2019.02.17
    吾尝终日而思矣——2019.02.12
  • 原文地址:https://www.cnblogs.com/kellyseeme/p/5525087.html
Copyright © 2011-2022 走看看