中卫网站推广优化公司,每月网站流量,WordPress考勤模板,上海网站建设友汇网站文章目录ansible模块ansible工具ansible代码验收ansible-hoc 常用模块命令说明命令执行的模块常用模块练习file模块练习copy模块练习yum_repository模块练习yum模块练习service模块练习systemd模块练习cron模块练习user模块练习group模块练习fetch模块练习get_url模块练习unarc…文章目录ansible模块ansible工具ansible代码验收ansible-hoc 常用模块命令说明命令执行的模块常用模块练习file模块练习copy模块练习yum_repository模块练习yum模块练习service模块练习systemd模块练习cron模块练习user模块练习group模块练习fetch模块练习get_url模块练习unarchive模块练习synchronize模块练习ansible模块ansible工具1.ansible****ansible ad-hoc 用来给指定主机清单设备推送单个任务类似Linux的基础命令 ansible[选项]主机清单-m 模块-a[模块参数]2.ansible-doc ansible模块的帮助文库-l 列出所有的模块3.ansible-galaxy 从ansible官方代码库下载成品playbook4.ansible-playbook*****剧本用来批量给指定主机清单推送多个任务类似Linux的shell脚本ansible代码验收ansible的工具再执行过程中通过输出代码的颜色来区分信息类别 绿色 正确信息没有操作 黄色 正确信息有操作 红色 报错信息要仔细分析 紫色 警告信息可能会造成影响也可以无影响ansible-hoc 常用模块使用ansible-hoc可以进行使用临时的命令做简单的批量任务多任务复杂任务需要使用playbook进行使用命令说明语法ansible 主机或组名字 -m 模块名字 -a ‘模块参数’ ansible参数主机和组 是在host文件中写好的模块名字 可以使用ansible-doc -l查看模块不写的时候是默认为command这个默认的模块可以在ansible.cfg中修改默认模块是不支持使用shell变量特殊符号的命令执行的模块#command模块加-a指定需要执行的命令就可以直接执行了不能加变量和特殊符合1、查看nfs主机当前的目录[rootm01 ~]# ansible nfs -m command -a pwdnfs|CHANGED|rc0/root2、查看nfs主机目前的主机名字[rootm01 ~]# ansible nfs -m command -a hostnamenfs|CHANGED|rc0nfs#shell模块语法和command差不多不过是使用/bin/sh进行执行的shell可以执行所有命令就像是在虚拟机一样shell模块的ansible参数 free_form: 需要执行的linux指令 chdir: 执行之前先切换到指定的目录默认是被管理节点的家目录类似cd creates: 一个文件名 当文件存在就不执行 removes: 一个文件名 当文件不存在就不执行1、在nfs主机的/tmp目录中创建test文件[rootm01 ~]# ansible nfs -m shell -a touch /tmp/test createstest[WARNING]: Consider using thefilemodule withstatetouch rather than runningtouch.If you need to usecommandbecausefileis insufficient you canaddwarn: falseto thiscommandtask orsetcommand_warningsFalseinansible.cfg to get rid of this message. nfs|CHANGED|rc0[rootm01 ~]# ansible nfs -m shell -a ls /tmp/testnfs|CHANGED|rc0/tmp/test#raw模块和shell用户是一样的不同的是他没有ansible参数可以使用script模块可以使用进行脚本文件并且不需要复制脚本文件到被控节点可以使用本机的脚步远程执行1、执行脚本查询nfs的ip网卡信息和磁盘信息#编辑脚本 给权限[rootm01 test]# cat ip_df.sh#!/bin/bashifconfigdf-Th[rootm01 test]# chmod x ip_df.sh#使用shell模块执行被控节点没有这个文件[rootm01 test]# ansible nfs -m shell -a ./test/ip_df.shnfs|FAILED|rc127/bin/sh: ./test/ip_df.sh: No suchfileor directorynon-zeroreturncode#使用script模块正常执行[rootm01 test]# ansible nfs -m script -a /test/ip_df.shnfs|CHANGED{changed:true,rc:0,stderr:Shared connection to nfs closed.\r\n,stderr_lines:[Shared connection to nfs closed.],常用模块练习file模块练习参数定义owner定义文件或目录的属主group定义文件或目录的组mode定义权限src要被链接的源文件需要使用statelink的情况使用dest被链接的文件需要使用statelink的情况使用force强制创建软链接file查看文件的状态touch创建不存在的文件absent删除文件、目录或取消链接文件 目录可以被递归删除directory创建目录 目录可以被递归创建link创建软连接hard创建硬链接1、在/tmp目录下创建test.txt 文件指定权限是644 属组和属主是root[rootm01 ~]# ansible nfs -m file -a path/tmp/test.txt statetouch mode644 ownerroot grouprootnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,dest:/tmp/test.txt,gid:0,group:root,mode:0644,owner:root,size:0,state:file,uid:0}2、查看test.txt的状态[rootm01 ~]# ansible nfs -m file -a path/tmp/test.txt statefilenfs|SUCCESS{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:false,gid:0,group:root,mode:0644,owner:root,path:/tmp/test.txt,size:0,state:file,uid:0}3、创建test.txt的软连接到/root/test[rootm01 ~]# ansible nfs -m file -a src/tmp/test.txt dest/root/test statelinknfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,dest:/root/test,gid:0,group:root,mode:0777,owner:root,size:13,src:/tmp/test.txt,state:link,uid:0}4、在/tmp目录下创建test1目录指定权限是744 属组和属主是root[rootm01 ~]# ansible nfs -m file -a path/tmp/test1 mode744 ownerroot grouproot statedirectorynfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,gid:0,group:root,mode:0744,owner:root,path:/tmp/test1,size:6,state:directory,uid:0}5、在/tmp目录下创建test1/abc/test2目录指定权限是744 属组和属主是root[rootm01 ~]# ansible nfs -m file -a path/tmp/test1/abc/test2 mode744 ownerroot grouproot statedirectorynfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,gid:0,group:root,mode:0744,owner:root,path:/tmp/test1/abc/test2,size:6,state:directory,uid:0}6、删除test1目录[rootm01 ~]# ansible nfs -m file -a path/tmp/test1/ stateabsentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,path:/tmp/test1/,state:absent}[rootm01 ~]#copy模块练习复制文件到远程主机src本地源文件路径可以使用绝对路径或当前目录的相对路径如果是目录会递归复制路径的结尾如果是/则只复制目录里面的内容不加/则是包含目录的全部内容dest远程主机的路径 使用绝对路径force如果复制的内容远程主机有但是内容不一样模块设置为yes会强制覆盖如果是no则只有不存在的时候复制存在不复制backup覆盖之前将原文件备份 yes|nocontent可以代替src 指定设置的文件的值others所有file模块里的参数选项这里都可以使用remote_src复制远程主机的文件1、将/test下的copy.TXT文件复制到nfs的/tmp下[rootm01 test]# ansible nfs -m copy -a src/test/copy.txt dest/tmpnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,checksum:73325af89b3946573099e254dfbc8ea35cde2072,dest:/tmp/copy.txt,gid:0,group:root,md5sum:51b0fa33360e53d54cad7d22c79c5814,mode:0644,owner:root,size:35,src:/root/.ansible/tmp/ansible-tmp-1765682359.38-33865-268993710447528/source,state:file,uid:0}2、将/test下的copy.TXT文件复制到nfs的/root指定权限是644 属组和属主是root[rootm01 test]# ansible nfs -m copy -a src/test/copy.txt dest/root ownerroot mode644 grouprootnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,checksum:73325af89b3946573099e254dfbc8ea35cde2072,dest:/root/copy.txt,gid:0,group:root,md5sum:51b0fa33360e53d54cad7d22c79c5814,mode:0644,owner:root,size:35,src:/root/.ansible/tmp/ansible-tmp-1765682416.62-34252-13625253724953/source,state:file,uid:0}3、将/test下的copy.TXT文件复制到nfs的/tmp 并且备份原文件[rootm01 test]# ansible nfs -m copy -a src/test/copy.txt dest/tmp ownerroot mode644 grouproot backupyesnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,checksum:73325af89b3946573099e254dfbc8ea35cde2072,dest:/tmp/copy.txt,gid:0,group:root,md5sum:51b0fa33360e53d54cad7d22c79c5814,mode:0644,owner:root,size:35,src:/root/.ansible/tmp/ansible-tmp-1765682474.77-34512-279332913259936/source,state:file,uid:0}4、在/tmp/copy.txt文件中写入just atest[rootm01 test]# ansible nfs -m copy -a contentjusr a test dest/tmp/copy.txtnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,checksum:d6e8657a4c27d357c6e9f4330d586679e9e7e806,dest:/tmp/copy.txt,gid:0,group:root,md5sum:c4f56579a02da4b9bd25abc12e555c50,mode:0644,owner:root,size:11,src:/root/.ansible/tmp/ansible-tmp-1765682615.44-35383-133423820636633/source,state:file,uid:0}[rootnfs tmp]# cat copy.txtjusr atestyum_repository模块练习此模块是用于管理yum仓库的file配置文件的名字不包含.reponame仓库名字description仓库描述信息baseurlyum的源地址enabled是否开启仓库 yes|nogpgcheck是否检查软件包的完整性 yes|nogpgcheck公钥地址1、为nfs部署一个本地源[rootm01 test]# ansible nfs -m yum_repository -a fileBaseos namebase baseurl/tmp/txt enabledyes gpgcheckno descriptionbaseosnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,repo:base,state:present}[rootm01 test]#yum模块练习使用yum管理软件包 安装name 操作的软件包的名字 也可以给url地址或者rpm包的本地路径state 状态 present安装 absent 卸载 latest最新版本1、安装httpd[rootm01 test]# ansible nfs -m yum -a namehttpd statepresentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,changes:{installed:[httpd]},2、卸载http[rootm01 test]# ansible nfs -m yum -a namehttpd stateabsentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,changes:{removed:[httpd]}service模块练习管理服务name 服务名称state对服务的操作 started启动 stopped关闭 restarted重启 reloaded 重新加载enabled是否开启启动 yes|no1、启动https服务并且设置开启自启[rootm01 test]# ansible nfs -m service -a namehttpd statestarted enabledyesnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,enabled:true,name:httpd,state:started,status:{systemd模块练习管理服务和service两个选择一个使用即可name 服务名称state 对服务的操作 started启动 stopped关闭 restarted重启 reloaded 重新加载enabled是否开启启动 yes|nodaemon_reload 当配置文件有变化就重新加载服务启动https服务[rootm01 ~]# ansible nfs -m systemd -a namehttpd statestartednfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,name:httpd,state:started,status:{cron模块练习name 任务的描述信息state 状态 present创建 absent删除user使用哪个用户执行day日hour时minute分month月weekday周job要执行的任务cron_file指定的配置文件1、星期二的12点使用root进行yum源清理 nfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,envs:[],jobs:[yum]}[rootnfs cron.d]# crontab -l*/5 * * * * /usr/sbin/chronyd -qserver ntp.aliyun.com iburst/dev/null#Ansible: yum012* *2yum clean all2、每2分钟向/tmp/test文件中写 test内容[rootm01 ~]# ansible nfs -m cron -a minute*/2 jobecho test /tmp/test statepresent[DEPRECATION WARNING]: Thenameparameter will be requiredinfuture releases..This feature will be removedinversion2.12. Deprecation warnings can be disabled by settingdeprecation_warningsFalseinansible.cfg. nfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,envs:[],jobs:[yum,None][rootnfs cron.d]# crontab -l*/5 * * * * /usr/sbin/chronyd -qserver ntp.aliyun.com iburst/dev/null#Ansible: yum012* *2yum clean all#Ansible: None*/2 * * * *echotest/tmp/test 删除定时任务[rootm01 ~]# ansible nfs -m cron -a nameyum stateabsentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,envs:[],jobs:[None]}user模块练习user模块是有useradd userdel usermod三个指令的state状态创建或者删除name用户名字uid用户uidgroup 用户组 groups附加组comment用户描述信息create_home是否创建家目录 yes|nohome指定用户家目录 和createhome一起使用shell用户的shell环境password指定密码在指定的时候不要使用明文可以先在外面将密码指定为加密字符串命令为echo “123456” |openssl passwd -1 -salt $(/dev/urandom tr -dc ‘[:alnum:]’|head -c 32) -stdin echo后面接想设置的密码removestate是删除的时候 并且remove是yes的时候就表示和家目录一起删除1、使用jinyin密码创建一个jy用户用户的uid是2000 附加组是root 创建家目录[rootm01 ~]# ansible nfs -m user -a namejy password$1$PopVwHlf$U4bqzwgtXl1SY328NeZwg1 uid2000 groupsroot create_homeyesnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,comment:,create_home:true,group:2000,groups:root,home:/home/jy,name:jy,password:NOT_LOGGING_PASSWORD,shell:/bin/bash,state:present,system:false,uid:2000}[rootnfs mail]# id jyuid2000(jy)gid2000(jy)groups2000(jy),0(root)2、删除jy用户[rootm01 ~]# ansible nfs -m user -a namejy removeyes stateabsent nfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,force:false,name:jy,remove:true,state:absent}group模块练习group也是一样有groupadd groupdel groupmodgid指定组idname 组名字state 状态1、创建jy组gid是2000[rootm01 ~]# ansible nfs -m group -a namejy gid2000 statepresentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,gid:2000,name:jy,state:present,system:false}2、删除组[rootm01 ~]# ansible nfs -m group -a namejy gid2000 stateabsentnfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,name:jy,state:absent}fetch模块练习此模块和copy是相反的是把被控端的文件复制到主控这边src 被控节点的路径dest主控的路径 必须以/结尾flat 默认是no 表示在主控这边是以被控的主机名的形式来组织目录结构的 yes则部署1、把nfs的/etc/passwd文件复制到主控的tmp目录下以被控的名字形式[rootm01 ~]# ansible nfs -m fetch -a src/etc/passwd dest/tmpnfs|CHANGED{changed:true,checksum:f24b7ecfe7d407867a4f11cad220cbbf3d699cab,dest:/tmp/nfs/etc/passwd,md5sum:61811292e958ed0f7ee2fc53ad5f6650,remote_checksum:f24b7ecfe7d407867a4f11cad220cbbf3d699cab,remote_md5sum:null}显示为[rootm01 nfs]# tree /tmp/nfs//tmp/nfs/ └── etc └──passwd2、把nfs的/etc/passwd文件复制到主控的tmp目录下不以被控的名字形式[rootm01 tmp]# ansible nfs -m fetch -a src/etc/passwd dest/tmp/ flatyesnfs|CHANGED{changed:true,checksum:f24b7ecfe7d407867a4f11cad220cbbf3d699cab,dest:/tmp/passwd,md5sum:61811292e958ed0f7ee2fc53ad5f6650,remote_checksum:f24b7ecfe7d407867a4f11cad220cbbf3d699cab,remote_md5sum:null}显示为[rootm01 tmp]# lspasswdvmware-root_772-2990547578 vmware-root_812-2957648972[rootm01 tmp]#get_url模块练习主要是用于http ftp https服务器上进行下载文件类似wgeturl 下载的urlusl_password usl_username 用于需要进行用户密码验证的时候dest 下载到哪里mode权限owner拥有人group拥有组1、下载阿里云的镜像到/tmp/samba下 权限为0440[rootm01 ~]# ansible nfs -m get_url -a urlhttps://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/samba-4.9.1-8.el8.x86_64.rpm dest/tmp/samba mode0440unarchive模块练习解压文件使用src 指定压缩文件的路径dest解压的路径 解压到哪remote_src yes文件会从主控复制到被控|no不复制直接在被控找压缩文件 默认是yesowner 解压之后文件或目录的拥有人group解压之后文件或目录的拥有组mode解压之后的权限creates 指定文件名字 文件存在就不解压list_filesyes列出压缩文件|no不列出 默认是no1、将打包的etc.tar.gz 解压缩到nfs的/tmp里面、 创建压缩文件[rootm01 ~]# tar -czvf /root/etc.tar.gz /etc/*[rootm01 ~]# ansible nfs -m unarchive -a src/root/etc.tar.gz dest/tmp/ nfs|CHANGED{ansible_facts:{discovered_interpreter_python:/usr/bin/python},changed:true,dest:/tmp/,extract_results:{cmd:[/usr/bin/gtar,--extract,-C,/tmp/,-z,-f,/root/.ansible/tmp/ansible-tmp-1765686820.51-58152-194285481444295/sourcesynchronize模块练习使用rsync进行同步文件src 要复制的文件路径 目录以/结尾就包含目录本身dest复制到哪里archive 归档 默认就是开启的 yes|nocompress 是否压缩 yes|norsync_opts rsyncd的参数选项 例如-v -a -z等等delete 删除不存在的文件 默认是nodest_port 默认目录主机上的端口默认是22 走ssh协议modepush或者pull push上传文件 pull下载 默认是push#将/etc目录上传到nfs的/tmp目录下[rootm01 ~]# ansible nfs -m synchronize -a src/etc/ dest/tmp/nfs|SUCCESS{changed:false,cmd:/usr/bin/rsync --delay-updates -F --compress --archive --rsh/usr/bin/ssh -S none -o StrictHostKeyCheckingno -o UserKnownHostsFile/dev/null --out-formatCHANGED%i %n%L /etc/ nfs:/tmp/,msg:,rc:0,stdout_lines:[]}