百度网盘0基础网站开发教程,网站文章编辑器代码,微信营销软件破解版,规模以上工业企业的标准是什么上午
ansible是⼀种由Python开发的⾃动化运维⼯具#xff0c;集合了众多运维⼯ 具#xff08;puppet、cfengine、chef、func、fabric#xff09;的优点#xff0c;实现了批量 系统配置、批量程序部署、批量运⾏命令等功能。
1、学习ansible的使用
ansible 主机ip|域名|组…上午
ansible是⼀种由Python开发的⾃动化运维⼯具集合了众多运维⼯ 具puppet、cfengine、chef、func、fabric的优点实现了批量 系统配置、批量程序部署、批量运⾏命令等功能。
1、学习ansible的使用
ansible 主机ip|域名|组名|别名 -m ping|copy... ‘参数’
1下载ansible软件包 [root1 ~]# yum -y install ansible
2创建ansible组
ansible通过⼀个主机清单功能来实现服务器分组。
Ansible的默认主机清单配置⽂件为/etc/ansible/hosts. [root1 ~]# vim /etc/ansible/hosts [group01]10.0.0.1210.0.0.1310.0.0.14[group02]10.0.0.1210.0.0.13 3执行ansible的ping命令
测试网络连通性
主机12和主机13都进行了免密主机14未进行免密 ssh-keygenssh-copy-id -i 10.0.0.12ssh-copy-id -i 10.0.0.13 [root1 ~]# ansible 10.0.0.12 -m ping10.0.0.12 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}[root1 ~]# ansible group02 -m ping10.0.0.12 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}10.0.0.13 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong} # 由于group01中的10.0.0.14主机未做免密操作所以会产生报错[root1 ~]# ansible group01 -m pingThe authenticity of host 10.0.0.14 (10.0.0.14) cant be established.ECDSA key fingerprint is SHA256:/p0PnfBbtW2E/MOWaBziXwCBEnuznbx1DiNgh1qJw.ECDSA key fingerprint is MD5:62:73:f0:9f:e3:b5:f1:d2:d4:73:b3:2e:1b:14:16:d0.Are you sure you want to continue connecting (yes/no)? yes10.0.0.14 | UNREACHABLE! {changed: false, msg: Failed to connect to the host via ssh: Warning: Permanently added 10.0.0.14 (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password)., unreachable: true}
4为主机设置ansible别名携带用户和密码
没有做免密登录的服务器可以指定⽤户名与密码 [root1 ~]# vim /etc/ansible/hostsother ansible_ssh_host10.0.0.14 ansible_ssh_port22 ansible_ssh_userroot ansible_ssh_pass1[group01]10.0.0.1210.0.0.13other[root1 ~]# ansible group01 -m ping10.0.0.13 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}10.0.0.12 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}other | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}[root1 ~]# ansible other -m pingother | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong}
5查看ansible帮助信息
查看所有⽀持的模块 [root1 ~]# ansible-doc -l 如果要查看ping模块的⽤法使⽤下⾯命令其它模块以此类推)# ansible-doc ping
6ansible hostname模块的使用
使用ansiblehostname模块批量修改主机名
注意: 它不能修改/etc/hosts⽂件)
基本格式为: ansible 操作的机器名或组名 -m 模块名 -a 参数 1值1 参数2值2 [root1 ~]# ansible group02 -m hostname -a nameab.li
7练习
不论你用哪种环境免密或者不免密端口是否22请最终将两台被管理机器加入到group1组即可 vim /etc/ansible/hostsweb01 ansible_ssh_host10.0.0.12 ansible_ssh_userroot ansible_ssh_pass1 ansible_ssh_port22 web02 ansible_ssh_host10.0.0.13 ansible_ssh_userroot ansible_ssh_pass1 ansible_ssh_port2222[group1]web01web02
下午
1、ansible介绍
ansible是基于模块⼯作的本身没有批量部署的能⼒。真正具有批量部署的是ansible所运⾏的模块ansible只是提供⼀种框架。
2、ansible file模块的使用
file模块⽤于对⽂件相关的操作(创建, 删除, 软硬链接等)
1创建目录 创建目录[root1 ~]# ansible group02 -m file -a path/tmp/abc/def statedirectory
2创建文件 [root1 ~]# ansible group1 -m file -a path/test/111 statetouch
3递归修改owner,group,mode [root1 ~]# ansible group02 -m file -a path/tmp/abc recurseyes ownerbin groupdaemon mode1777[root2 ~]# ll /tmp/总用量 0drwxrwxrwt. 3 bin daemon 17 8月 16 14:09 abc[root2 ~]# ll /tmp/abc/总用量 0drwxrwxrwt. 2 bin daemon 6 8月 16 14:09 def
4删除⽬录
连同⽬录⾥的所有⽂件) [root1 ~]# ansible group02 -m file -a path/tmp/abc stateabsent
5创建⽂件并指定owner,group,mode等 [root1 ~]# ansible group02 -m file -a path/tmp/aaa statetouch ownerbin groupdaemon mode1777
6创建软链接⽂件
软链接指向硬链接硬链接指向文件 [root1 ~]# ansible group02 -m file -a src/etc/fstab path/tmp/xxx statelink [root2 ~]# ll /tmp/lrwxrwxrwx. 1 root root 10 8月 16 14:30 xxx - /etc/fstab
7创建硬链接⽂件 [root1 ~]# ansible group02 -m file -a src/etc/fstab path/tmp/xxx2 statehard[root2 ~]# ll /tmp/-rw-r--r--. 2 root root 502 7月 23 03:23 xxx2
3、ansible stat模块的使用
stat模块类似linux的stat命令⽤于获取⽂件的状态信息。
获取/etc/fstab⽂件的状态信息 [root1 ~]# ansible group02 -m stat -a path/etc/fstab
4、ansible copy模块的使用
copy模块⽤于对⽂件的远程拷⻉操作如把本地的⽂件拷⻉到远程的机器上)
1拷贝文件
拷⻉mysql文件到10.0.0.12主机上 [root1 ~]# ansible 10.0.0.12 -m copy -a src/root/mysql57.tar.gz dest/root/[root1 ~]# ansible group02 -m copy -a src/etc/fstab dest/tmp/a.txt backupyes ownerbin groupdaemon mode1777
2远程向文件中写入数据
使⽤content参数默认会覆盖原内容 [root1 ~]# ansible group01 -m copy -a contentwo dest~/tst# 注意:ansible中-a后⾯的参数⾥也有引号时记得要单引双引交叉使⽤如果都为双引会出现问题
3force参数的使用是否强制覆盖执行命令
如果⽬标⽂件已经存在则会强制覆盖forceyes
如果⽬标⽂件已经存在则不覆盖forceno [root1 ~]# touch tst[root1 ~]# ansible group02 -m copy -a src./tst dest/root/ forceno[root1 ~]# dd if/dev/zero oftst1 bs100M count1[root1 ~]# ansible group01 -m copy -a src./tst1 dest/root/ forceyes[root1 ~]# ansible group01 -m copy -a contentwo dest~/tst forceno# 不执行写入操作操作无意义
4backup参数控制是否备份⽂件 [root1 ~]# ansible group01 -m copy -a src./tst dest~ backupyes ownerbin groupdaemon mode1777# backupyes表示如果拷⻉的⽂件内容与原内容不⼀样则会备份⼀份将原来的文件修改文件名变成备份文件
5拷贝目录
copy模块拷⻉时要注意拷⻉⽬录后⾯是否带/符号
后⾯不带/符号则表示把整个⽬录拷⻉过去
后⾯带/符号则表示把⽬录⾥的所有⽂件拷⻉过去 [root1 ~]# ansible group01 -m copy -a src/etc/yum.repos.d dest/etc/yum.repos.d/ backupyes[root2 ~]# ls /etc/yum.repos.d/CentOS-Base.repo epel.repo epel-testing.repo hh.repo repo.tar.gz yum.repos.d
5、ansible template模块的使用
与copy模块功能⼏乎⼀样
template模块⾸先使⽤变量渲染jinja2模板⽂件成普通⽂件,然后再复制过去.⽽copy模块不⽀持.(jinja2是⼀个基于python的模板引擎)
ansible.builtin.template module – Template a file out to a target host — Ansible Community Documentation ansible -m template group01 -a src/etc/hosts dest/tmp/hosts
template模块不能拷⻉⽬录 ansible -m template group01 -a src/etc/yum.repos.d/ dest/etc/yum.repos.d/
6、ansible fatch模块的使用
fetch模块与copy模块类似但作⽤相反。⽤于把远程机器的⽂件拷⻉到本地收文件。
注意: fetch模块不能从远程拷⻉⽬录到本地 [root1 ~]# ansible group01 -m fetch -a src/etc/sysconfig/network-scripts/ifcfg-ens33 dest/tmp# 因为group01⾥有3台机器,为了避免同名⽂件⽂件冲突它使⽤了不同的⽬录)[root1 ~]# tree /tmp//tmp/├── 10.0.0.12│ └── etc│ └── sysconfig│ └── network-scripts│ └── ifcfg-ens33├── 10.0.0.13│ └── etc│ └── sysconfig│ └── network-scripts│ └── ifcfg-ens33├── other│ └── etc│ └── sysconfig│ └── network-scripts│ └── ifcfg-ens33
7、ansible user模块的使用
user模块⽤于管理⽤户账号和⽤户属性。
1创建普通用户 [root1 ~]# ansible group02 -m user -a nameaaa statepresent
2创建系统用户并设置shell环境为/sbin/nologin [root1 ~]# ansible group01 -m user -a namemysql statepresent systemyes shell/sbin/nologin
3创建mysql账户并指定uid和密码 # 创建mysql家目录[root1 ~]# ansible group01 -m file -a path/usr/local/mysql statedirectory# 创建mysql-files目录并修改文件的权限和所属主与组[root1 ~]# ansible group01 -m file -a path/usr/local/mysql/mysql-files statedirectory ownermysql groupmysql mode750 [root1 ~]# ansible group01 -m user -a nameabc statepresent uid1999 passwordabc
生成加密密码 [root1 ~]# echo 123456 | openssl passwd -1 -stdin$1$YxSnSopH$0t2l5RUA4m9JKlmjVZbta.
4创建普通用户,并产⽣空密码密钥对 [root1 ~]# ansible group01 -m user -a namehadoop generate_ssh_keyyes
5删除用户默认不删除家目录 [root1 ~]# ansible group01 -m user -a namehadoop stateabsent[root2 ~]# ll /home/总用量 0drwx------. 2 aaa aaa 62 8月 16 16:03 aaadrwx------. 2 abc abc 62 8月 16 16:14 abcdrwx------. 3 2000 2000 74 8月 16 16:20 hadoopdrwx------. 2 mysql mysql 62 8月 16 16:06 mysql
7删除用户的同时删除家目录
使⽤removeyes参数 [root1 ~]# ansible group01 -m user -a nameaaa stateabsent removeyes
8、ansible group模块的使用
group模块⽤于管理⽤户组和⽤户组属性。
1创建组 ansible group01 -m group -a namegroupa gid3000 statepresent
2删除组
如果有⽤户的gid为此组则删除不了 ansible group01 -m group -a namegroupa stateabsent
9、ansible cron模块的使用
cron模块⽤于管理周期性时间任务
创建⼀个cron任务,不指定user的话,默认就是root因为这⾥是⽤root用户操作的)。
如果minute,hour,day,month,week不指定的话默认都为*
1添加计划任务 [root1 ~]# ansible group01 -m cron -a nameabc userroot job/usr/sbin/ntpdate cn.ntp.org.cn hour2
2删除cron任务 [root1 ~]# ansible group01 -m cron -a nameabc stateabsent
10、ansible yum_repository模块的使用
yum_repository模块⽤于配置yum仓库。
1增加⼀个/etc/yum.repos.d/local.repo配置⽂件
注意此模块只帮助配置yum仓库,但如果仓库⾥没有软件包安装⼀ 样会失败。
所以可以⼿动去挂载光驱到/mnt⽬录
mount /dev/cdrom /mnt ansible group01 -m yum_repository -a namelocal descriptionlocalyum baseurlfile:///mnt/ enabledyes gpgcheckno
2删除/etc/yum.repos.d/local.repo配置⽂件 ansible group01 -m yum_repository -a namelocal stateabsent
11、ansible yum模块的使用
yum模块⽤于使⽤yum命令来实现软件包的安装与卸载。
1安装软件 [root1 ~]# ansible group01 -m yum -a namentpdate statepresent
statelatest表示安装最新版本 ansible group01 -m yum -a namehttpd,httpddevel statelatest
2卸载软件 ansible group1 -m yum -a namehttpd,httpddevel stateabsent
12、ansible service模块的使用
service模块⽤于控制服务的启动,关闭,开机⾃启动等。
1启动服务 # 启动vsftpd服务并设为开机⾃动启动ansible group01 -m service -a namevsftpd statestarted enabledon
2关闭服务 # 关闭filewalld服务并设为开机不⾃动启动[root1 ~]# ansible group01 -m service -a namefirewalld statestopped enabledfalse
13、ansible script模块的使用
script模块⽤于在远程机器上执⾏本地脚本。
此脚本不⽤给执⾏权限) [root1 ~]# vim /tmp/1.sh[root1 ~]# ansible group01 -m script -a /tmp/1.sh
练习使⽤shell脚本实现在group01的被管理机⾥的mariadb⾥创建⼀ 个abc库 [root1 ~]# vim /tmp/2.sh#!/bin/bashyum install mariadb-server -y /dev/nullsystemctl start mariadbsystemctl enable mariadbmysql EOFcreate database abc;quitEOF[root1 ~]# ansible group01 -m script -a /tmp/2.sh
14、ansible command模块与shell模块的使用
两个模块都是⽤于执⾏linux命令的,这对于命令熟悉的⼯程师来说⽤起来⾮常high。
shell模块与command模块差不多command模块不能执⾏⼀些类似 $HOME,,,|等符号但shell可以)
1使用command模块创建用户 [root1 ~]# ansible group01 -m command -a useradd user1[root1 ~]# ansible group01 -m command -a id user110.0.0.13 | CHANGED | rc0 uid2000(user1) gid2000(user1) 组2000(user1)10.0.0.12 | CHANGED | rc0 uid2000(user1) gid2000(user1) 组2000(user1)other | CHANGED | rc0 uid2000(user1) gid2000(user1) 组2000(user1)
2使用shell模块使用管道符进行统计查询信息 [root1 ~]# ansible group01 -m shell -a cat /etc/passwd | wc -l10.0.0.13 | CHANGED | rc0 2210.0.0.12 | CHANGED | rc0 22other | CHANGED | rc0 22
3使用shell模块使用$HOME变量 [root1 ~]# ansible -m shell group01 -a cd $HOME;pwd10.0.0.13 | CHANGED | rc0 /rootother | CHANGED | rc0 /root10.0.0.12 | CHANGED | rc0 /root
4使用command模块进行关机 [root1 ~]# ansible group01 -m command -a shutdown -h 010.0.0.12 | FAILED | rc-1 Failed to connect to the host via ssh: ssh: connect to host 10.0.0.12 port 22: Connection refusedother | FAILED | rc-1 Failed to connect to the host via ssh: ssh_exchange_identification: read: Connection reset by peer10.0.0.13 | CHANGED | rc0 Shutdown scheduled for 五 2024-08-16 16:49:28 CST, use shutdown -c to cancel.
注意: shell模块并不是百分之百任何命令都可以,⽐如vim或ll别名就不可以。 文章转载自: http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.httzf.cn.gov.cn.httzf.cn http://www.morning.sfphz.cn.gov.cn.sfphz.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.nfzw.cn.gov.cn.nfzw.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.fyskq.cn.gov.cn.fyskq.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.wklyk.cn.gov.cn.wklyk.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn