ansible命令行常用模块

1.shell 模块

此模块用于在各被管理节点运行指定的命令
[root@xinlibao tmp]# ansible test -m shell -a 'cd /tmp/ && touch 11.txt'
172.20.35.239 | SUCCESS | rc=0 >>
 
 
[root@xinlibao tmp]# ansible test -m shell -a 'ls /tmp'
172.20.35.239 | SUCCESS | rc=0 >>
11.txt

2.copy 模块

需要注意的是节点如果开启了selinux ,copy模块的使用必须安装 libselinux-python 安装方式:yum -y install libselinux-python
此模块为远程复制备份模块
模块参数详解:
src:指定源文件路径,可以是相对路径,也可以是绝对路径,可以是目录(并非是必须的,可以使用content,直接生成文件内容)
dest=:指定目标文件路径,只能是绝对路径,如果src是目录,此项必须是目录
owner:指定属主
group:指定属组
mode:指定权限,可以以数字指定比如0644
backup:在覆盖之前将原文件备份,备份文件包含时间信息。有两个选项:yes|no
force:
    yes:默认项,如果目标主机包含该文件,但内容不同,则强制覆盖
    no:则只有当目标主机的目标位置不存在该文件时,才复制
directory_mode:递归的设定目录的权限,默认为系统默认权限
实例:
[root@xinlibao tmp]# ansible test -m copy -a 'src=/tmp/test.txt dest=/tmp'  #src是本地的文件,dest是远程文件应该复制的绝对路径。
172.20.35.239 | SUCCESS => {
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/tmp/test.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1516346061.29-144857595536336/source",
    "state": "file",
    "uid": 0
}
 
#dest解释
Remote absolute path where the file should be copied to. If src is a directory, this must be a directory too. If dest is a nonexistent path and if
either dest ends with "/" or src is a directory, dest is created. If src and dest are files, the parent directory of dest isn't created: the task
fails if it doesn't already exist.
远程文件应该复制到的绝对路径。如果src是一个目录,那么它也必须是一个目录。如果dest是不存在的路径,并且如果dest以“/”结尾或者src是目录,则dest被创建。
如果src和dest是文件,那么不会创建dest的父目录:如果任务不存在,任务将失败

3.file 模块

对远程文件管理的模块
模块参数详解:
owner:修改属主
group:修改属组
mode:修改权限
path=:要修改文件的路径
recurse:递归的设置文件的属性,只对目录有效
  yes:表示使用递归设置
state:
 touch:创建一个新的空文件
 directory:创建一个新的目录,当目录存在时不会进行修改
 link:创建软连接,结果src一起使用此选项才生效
 hard:创建硬连接
 absent:删除文件,目录,软连接
实例:
[root@xinlibao tmp]# ansible test -m file -a 'dest=/tmp/aa.sh mode=777 owner=xin group=xin'
172.20.35.239 | SUCCESS => {
    "changed": false,
    "gid": 501,
    "group": "xin",
    "mode": "0777",
    "owner": "xin",
    "path": "/tmp/aa.sh",
    "size": 19,
    "state": "file",
    "uid": 501
}
#创建一个文件
[root@xinlibao tmp]# ansible test -m file -a 'path=/tmp/xlb.txt state=touch'
#递归设置文件的属主或者属组
[root@xinlibao tmp]# ansible test -m file -a 'path=/tmp/xlb owner=root group=root recurse=yes'
#为文件设置软连接
[root@xinlibao tmp]# ansible test -m file -a 'src=/tmp/xinlibao state=link path=/tmp/xin'

4.template 模块

ansible的template模块,可以将带有参数的配置文件传递到目标地址,可以对文件进行属组属主的修改以及备份。
参数详解:
dest:远程节点上的绝对路径,用于放置template文件
src:本地Jinjia2模版的template文件位置
group:设置远程节点上的的template文件的所属用户组
owner:设置远程节点上的template文件所属用户
mode:设置远程节点上的template文件权限
官方文档:
Templates are processed by the Jinja2 templating language (http://jinja.pocoo.org/docs/) - documentation on the template formatting can be found in the Template Designer Documentation (http://jinja.pocoo.org/docs/templates/).
Six additional variables can be used in templates: ansible_managed (configurable via the defaults section of ansible.cfg) contains a string which can be used to describe the template name, host, modification time of the template file and the owner uid. template_host contains the node name of the template’s machine. template_uid the numeric user id of the owner. template_path the path of the template. template_fullpath is the absolute path of the template. template_run_date is the date that the template was rendered.
实例:
[root@xinlibao ansible]# ansible test -m template -a 'src=/etc/ansible/hosts dest=/tmp/ owner=root group=root mode=0644'
10.0.0.5 | SUCCESS => {
    "changed": true,
    "checksum": "d0f824ed0d55618ae4ef58a75917b19dad896193",
    "dest": "/tmp/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "606dfb055212a670f0fe03eb8510d1d5",
    "mode": "0644",
    "owner": "root",
    "size": 16,
    "src": "/root/.ansible/tmp/ansible-tmp-1516545559.76-271687129655779/source",
    "state": "file",
    "uid": 0
}

5.lineinfile 模块

此模块为文件编辑模块
模块参数详解:
  path:指定要修改的配置文件
  regexp:匹配要修改的内容
  line:要增加或者修改的内容
  state:
      absent:表示删除,当匹配到时进行删除
      present:表示增加,当匹配到时进行修改,当没有匹配到时在最后增加一行,默认为此项
  backrefs:
      no:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;
      yes:表示如果没有匹配到,则不变line;如果匹配成功,则替换line;
  backup: 
      no:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;不备份原文件
      yes:表示如果没有匹配到,则增加line;如果匹配成功,则替换line;备份原文件
  insertafter:
      在匹配到的行之后添加一行
  insertbefore:
        在匹配到的行之前添加一行
实例:
[root@xinlibao ansible]# ansible test -m lineinfile -a 'path=/tmp/test.txt regexp="11111" line=aaaaa backrefs=no'
10.0.0.5 | SUCCESS => {
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@xinlibao ansible]# ansible test -m lineinfile -a 'path=/tmp/test.txt regexp="4444" line=bbbb backrefs=yes'
10.0.0.5 | SUCCESS => {
    "backup": "",
    "changed": false,
    "msg": ""
}

6.service

service为服务模块
模块参数详解:
enabled:表示设置服务开机是否启动,enabled=yes
name:表示要控制哪一个服务
state:
     started:表示现在就启动此服务
     stopped:表示现在关闭此服务
     restarted:表示重启此服务
sleep:如果执行了restarted,在stop和start之间沉睡几秒
[root@xinliao ~]#ansible test -m service -a 'enabled=on name=httpd state=started'

7.cron 模块

此模块是创建计划任务
模块参数详解:
state:
 present:创建任务
 absent:删除任务
backup:对远程主机上的原任务计划内容修改之前做备份
job:要执行的任务
name:该任务的描述
minute:分钟(0-59,*,*/2,……),不写默认为*
hour:小时(0-23,*,*/2,……),不写默认为*
day:日(1-31,*,*/2,……),不写默认为*
month:月(1-12,*,*/2,……),不写默认为*
weekday:周(0-7,*,……),不写默认为*
实例:
[root@xinlibao tmp]# ansible test -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/sh aa.sh"'
172.20.35.239 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "custom job"
    ]
}
[root@xinlibao tmp]# ansible test -a 'crontab -l'
172.20.35.239 | SUCCESS | rc=0 >>
#time sync by aige at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
#Ansible: custom job
*/3 * * * * /usr/sbin/sh aa.sh

8.user/group 模块

user为管理用户的模块
 name:指定用户名
 password:设定用户密码,password参数需要接受md5加密后的值
 state:用户状态,默认为present
    present:表示添加用户
    absent:表示删除用户
system:
    yes:默认创建为普通用户,而非系统用户
  如果不指定默认生成的选项有:
     home:创建家目录
     shell:创建默认的shell为/bin/bash
remove:
    yes:删除用户家目录,需要指定此参数
    no:默认项,删除用户时默认不删除用户的家目录
update_password:修改用户密码
    always:新密码和旧密码不同时进行修改
    on_create:为新创建的用户指定密码
 
#实例:创建一个用户。
[root@xinlibao ansible]# echo 123456 | openssl passwd -1 -stdin
$1$N9pnVurO$n4fDhg.X2kWcy0WwWaZLo1
[root@xinlibao ansible]# ansible test -m user -a 'name=xin system=yes password=$1$N9pnVurO$n4fDhg.X2kWcy0WwWaZLo1'
10.0.0.5 | SUCCESS => {
    "changed": true,
    "comment": "",
    "createhome": true,
    "group": 992,
    "home": "/home/xin",
    "name": "xin",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 995
}
实例:删除一个用户
[root@xinlibao ansible]# ansible test -m user -a 'name=xin remove=yes state=absent'
10.0.0.5 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "xin",
    "remove": true,
    "state": "absent",
    "stderr": "userdel: xin mail spool (/var/spool/mail/xin) not found\n",
    "stderr_lines": [
        "userdel: xin mail spool (/var/spool/mail/xin) not found"
    ]
}
实例:更新一个用户的密码
ansible test -m user -a 'name=liuwei1 update_password=always password=$1$N9pnVurO$n4fDhg.X2kWcy0WwWaZLo1'

9.setup 模块

此模块是收集远程主机信息。
收集可用的facts,收集每个节点的相关信息:架构信息,IP,时间,域名,网卡,MAC,主机名,CPU等信息。
这些收集的信息,可以作为变量。
ansible test -m setup

10.script 模块

此模块为在远程主机执行本地脚本
[root@xinlibao tmp]# ansible test -m script -a '/tmp/cc.sh'
172.20.35.239 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.20.35.239 closed.\r\n",
    "stdout": "",
    "stdout_lines": []
}

11.unarchive 模块

unarchive模块为解压缩,这个模块有两种用法,一种是解压本地的压缩文件,另一种是解压远程主机上的压缩文件。
参数:
copy:默认为yes,当copy=yes,那么拷贝的文件是从ansible主机复制到远程主机上的,如果设置为copy=no,那么会在远程主机上寻找src源文件。
src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no。
dest:远程主机上的目标路径。
mode:设置解压缩后的文件权限。
 
实例1:解压本地的压缩文件
[root@netpas-lab7 ansible]# ansible centos5 -m unarchive -a 'src=/etc/ansible/s.tgz dest=/xlb/test/ copy=yes mode=0755 owner=root group=root'
172.20.33.151 | SUCCESS => {
    "changed": true,
    "dest": "/xlb/test/",
    "extract_results": {
        "cmd": [
            "/bin/gtar",
            "--extract",
            "-C",
            "/xlb/test/",
            "-z",
            "--owner=root",
            "--group=root",
            "-f",
            "/root/.ansible/tmp/ansible-tmp-1520215977.01-169254067586872/source"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "failed": false,
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "size": 4096,
    "src": "/root/.ansible/tmp/ansible-tmp-1520215977.01-169254067586872/source",
    "state": "directory",
    "uid": 0
}
 
验证:
[root@netpas-lab7 ansible]# ansible centos5 -m shell -a 'ls -l /xlb/test/'
172.20.33.151 | SUCCESS | rc=0 >>
总计 4
drwxr-xr-x 7 root root 4096 01-25 11:13 service
 
实例2:解压远程主机上的压缩文件
[root@netpas-lab7 ansible]# ansible centos5 -m unarchive -a 'src=/xlb/test/s.tgz dest=/xlb/test/ copy=no mode=0755 owner=root group=root'
172.20.33.151 | SUCCESS => {
    "changed": true,
    "dest": "/xlb/test/",
    "extract_results": {
        "cmd": [
            "/bin/gtar",
            "--extract",
            "-C",
            "/xlb/test/",
            "-z",
            "--owner=root",
            "--group=root",
            "-f",
            "/xlb/test/s.tgz"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "failed": false,
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "size": 4096,
    "src": "/xlb/test/s.tgz",
    "state": "directory",
    "uid": 0
}
 
验证:
[root@netpas-lab7 ansible]# ansible centos5 -m shell -a 'ls -l /xlb/test/'
172.20.33.151 | SUCCESS | rc=0 >>
总计 24
drwxr-xr-x 7 root root  4096 01-25 11:13 service
-rw-r--r-- 1 root root 18533 03-05 10:19 s.tgz

  

posted @ 2018-04-04 10:29  辛立宝  阅读(333)  评论(0编辑  收藏  举报