(第三十天)

上午
ansible 是⼀种由 Python 开发的⾃动化运维⼯具,集合了众多运维⼯ 具( puppet cfengine
chef func fabric )的优点,实现了批量 系统配置、批量程序部署、批量运⾏命令等功能。
1 、学习 ansible 的使用
ansible 主机 ip| 域名 | 组名 | 别名 -m ping|copy... ‘ 参数
1 )下载 ansible 软件包
[root@1 ~] # yum -y install ansible
2 )创建 ansible
ansible 通过⼀个主机清单功能来实现服务器分组。
Ansible 的默认主机清单配置⽂件为 /etc/ansible/hosts.
[root@1 ~] # vim /etc/ansible/hosts
[group01]
10 .0.0.12
10 .0.0.13
10 .0.0.14
[group02]
10 .0.0.12
10 .0.0.13
3 )执行 ansible ping 命令
(测试网络连通性)
主机 12 和主机 13 都进行了免密,主机 14 未进行免密
ssh-keygen
ssh-copy-id -i 10 .0.0.12
ssh-copy-id -i 10 .0.0.13
[root@1 ~] # ansible 10.0.0.12 -m ping
10 .0.0.12 | SUCCESS = > {
"ansible_facts" : {
"discovered_interpreter_python" : "/usr/bin/python"
},
"changed" : false ,
"ping" : "pong"
}
[root@1 ~] # ansible group02 -m ping
10 .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 主机未做免密操作,所以会产生报错
[root@1 ~] # ansible group01 -m ping
The authenticity of host '10.0.0.14 (10.0.0.14)' can 't be established.
ECDSA key fingerprint is SHA256:/p0PnfBbtW2E/MOWaBziXwCBEnuznb + x1DiNgh1 + qJw.
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)? yes
10 .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 别名(携带用户和密码)
没有做免密登录的服务器可以指定⽤户名与密码
[root@1 ~] # vim /etc/ansible/hosts
other ansible_ssh_host = 10 .0.0.14 ansible_ssh_port = 22 ansible_ssh_user = root
ansible_ssh_pass = 1
[group01]
10 .0.0.12
10 .0.0.13
other
[root@1 ~] # ansible group01 -m ping
10 .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"
}
[root@1 ~] # ansible other -m ping
other | SUCCESS = > {
"ansible_facts" : {
"discovered_interpreter_python" : "/usr/bin/python"
},
"changed" : false ,
"ping" : "pong"
}
5 )查看 ansible 帮助信息
查看所有⽀持的模块
[root@1 ~] # ansible-doc -l
如果要查看 ping 模块的 法,使 命令(其它模块以此类推 )
# ansible-doc ping
6 ansible hostname 模块的使用
使用 ansible hostname 模块)批量修改主机名
(注意 : 它不能修改 /etc/hosts ⽂件 )
基本格式为 : ansible 操作的机器名或组名 -m 模块名 -a " 参数 1= 1 参数 2= 2"
[root@1 ~] # ansible group02 -m hostname -a 'name=ab.li'
7 )练习
不论你用哪种环境(免密或者不免密,端口是否 22 ),请最终将两台被管理机器加入到 group1 组即可
vim /etc/ansible/hosts
web01 ansible_ssh_host = 10 .0.0.12 ansible_ssh_user = root ansible_ssh_pass = 1
ansible_ssh_port = 22
web02 ansible_ssh_host = 10 .0.0.13 ansible_ssh_user = root ansible_ssh_pass = 1
ansible_ssh_port = 2222
[group1]
web01
web02
下午
1 ansible 介绍
ansible 是基于模块⼯作的,本身没有批量部署的能⼒。真正具有批量部署的是 ansible 所运⾏的模
块, ansible 只是提供⼀种框架。
2 ansible file 模块的使用
file 模块⽤于对⽂件相关的操作 ( 创建 , 删除 , 软硬链接等 )
1 )创建目录
创建目录
[root@1 ~] # ansible group02 -m file -a 'path=/tmp/abc/def state=directory'
2 )创建文件
[root@1 ~]# ansible group1 -m file -a 'path=/test/111 state=touch'
3 )递归修改 owner,group,mode
[root@1 ~]# ansible group02 -m file -a 'path=/tmp/abc recurse=yes owner=bin
group=daemon mode=1777'
[root@2 ~]# ll /tmp/
总用量 0
drwxrwxrwt. 3 bin daemon 17 8 16 14:09 abc
[root@2 ~]# ll /tmp/abc/
总用量 0
drwxrwxrwt. 2 bin daemon 6 8 16 14:09 def
4 )删除⽬录
(连同⽬录⾥的所有⽂件 )
[root@1 ~]# ansible group02 -m file -a 'path=/tmp/abc state=absent'
5 )创建⽂件并指定 owner,group,mode
[root@1 ~]# ansible group02 -m file -a 'path=/tmp/aaa state=touch owner=bin
group=daemon mode=1777'
6 )创建软链接⽂件
(软链接指向硬链接,硬链接指向文件)
[root@1 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx
state=link'
[root@2 ~]# ll /tmp/
lrwxrwxrwx. 1 root root 10 8 16 14:30 xxx -> /etc/fstab
7 )创建硬链接⽂件
[root@1 ~]# ansible group02 -m file -a 'src=/etc/fstab path=/tmp/xxx2
state=hard'
[root@2 ~]# ll /tmp/
-rw-r--r--. 2 root root 502 7 23 03:23 xxx2
3 ansible stat 模块的使用
stat 模块类似 linux stat 命令,⽤于获取⽂件的状态信息。
获取 /etc/fstab ⽂件的状态信息
[root@1 ~] # ansible group02 -m stat -a 'path=/etc/fstab'
4 ansible copy 模块的使用
copy 模块⽤于对⽂件的远程拷⻉操作(如把本地的⽂件拷⻉到远程的机器上 )
1 )拷贝文件
拷⻉ mysql 文件到 10.0.0.12 主机上
[root@1 ~] # ansible 10.0.0.12 -m copy -a 'src=/root/mysql57.tar.gz
dest=/root/'
[root@1 ~] # ansible group02 -m copy -a 'src=/etc/fstab dest=/tmp/a.txt
backup=yes owner=bin group=daemon mode=1777'
2 )远程向文件中写入数据
使⽤ content 参数(默认会覆盖原内容)
[root@1 ~] # ansible group01 -m copy -a 'content="wo" dest=~/tst'
# 注意 :ansible -a 的参数 也有引号时,记得要单引双引交叉使 ,如果都为双引会出现问题
3 force 参数的使用,是否强制覆盖执行命令
如果⽬标⽂件已经存在,则会强制覆盖( force=yes
如果⽬标⽂件已经存在,则不覆盖( force=no
[root@1 ~] # touch tst
[root@1 ~] # ansible group02 -m copy -a 'src=./tst dest=/root/ force=no'
[root@1 ~] # dd if=/dev/zero of=tst1 bs=100M count=1
[root@1 ~] # ansible group01 -m copy -a 'src=./tst1 dest=/root/ force=yes'
[root@1 ~] # ansible group01 -m copy -a 'content="wo" dest=~/tst force=no'
# 不执行写入操作,操作无意义
4 backup 参数控制是否备份⽂件
[root@1 ~] # ansible group01 -m copy -a 'src=./tst dest=~ backup=yes owner=bin
group=daemon mode=1777'
# backup=yes 表示如果拷 件内容与原内容不 样,则会备份 份(将原来的文件修改文件名变成备份文件)
5 )拷贝目录
copy 模块拷⻉时要注意拷⻉⽬录后⾯是否带 "/" 符号
后⾯不带 / 符号,则表示把整个⽬录拷⻉过去
后⾯带 / 符号,则表示把⽬录⾥的所有⽂件拷⻉过去
[root@1 ~] # ansible group01 -m copy -a 'src=/etc/yum.repos.d
dest=/etc/yum.repos.d/ backup=yes'
[root@2 ~] # 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 的模板引擎 )
https://docs.ansible.com/ansible/latest/modules/template_module.html#template-module
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 模块不能从远程拷⻉⽬录到本地
[root@1 ~] # ansible group01 -m fetch -a 'src=/etc/sysconfig/network
scripts/ifcfg-ens33 dest=/tmp'
# 因为 group01 3 台机器 , 为了避免同名 件冲突,它使 了不同的 )
[root@1 ~] # 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 )创建普通用户
[root@1 ~] # ansible group02 -m user -a 'name=aaa state=present'
2 )创建系统用户,并设置 shell 环境为 /sbin/nologin
[root@1 ~] # ansible group01 -m user -a 'name=mysql state=present system=yes
shell="/sbin/nologin"'
3 )创建 mysql 账户,并指定 uid 和密码
# 创建 mysql 家目录
[root@1 ~] # ansible group01 -m file -a 'path=/usr/local/mysql state=directory'
# 创建 mysql-files 目录并修改文件的权限和所属主与组
[root@1 ~] # ansible group01 -m file -a 'path=/usr/local/mysql/mysql-files
state=directory owner=mysql group=mysql mode=750'
[root@1 ~] # ansible group01 -m user -a 'name=abc state=present uid=1999
password=abc'
生成加密密码
[root@1 ~] # echo 123456 | openssl passwd -1 -stdin
$1$YxSnSopH$0 t2l5RUA4m9JKlmjVZbta.
4 )创建普通用户 , 并产⽣空密码密钥对
[root@1 ~] # ansible group01 -m user -a 'name=hadoop generate_ssh_key=yes'
5 )删除用户,默认不删除家目录
[root@1 ~] # ansible group01 -m user -a 'name=hadoop state=absent'
[root@2 ~] # ll /home/
总用量 0
drwx------. 2 aaa aaa 62 8 16 16 :03 aaa
drwx------. 2 abc abc 62 8 16 16 :14 abc
drwx------. 3 2000 2000 74 8 16 16 :20 hadoop
drwx------. 2 mysql mysql 62 8 16 16 :06 mysql
7 )删除用户的同时删除家目录
使⽤ remove=yes 参数
[root@1 ~] # ansible group01 -m user -a 'name=aaa state=absent remove=yes'
8 ansible group 模块的使用
group 模块⽤于管理⽤户组和⽤户组属性。
1 )创建组
ansible group01 -m group -a 'name=groupa
gid = 3000 state = present '
2 )删除组
如果有⽤户的 gid 为此组,则删除不了
ansible group01 -m group -a 'name=groupa
state = absent '
9 ansible cron 模块的使用
cron 模块⽤于管理周期性时间任务
创建⼀个 cron 任务 , 不指定 user 的话 , 默认就是 root (因为这⾥是⽤ root 用户操作的 )
如果 minute,hour,day,month,week 不指定的话,默认都为 *
1 )添加计划任务
[root@1 ~] # ansible group01 -m cron -a 'name="abc" user=root
job="/usr/sbin/ntpdate cn.ntp.org.cn" hour=2'
2 )删除 cron 任务
[root@1 ~] # ansible group01 -m cron -a 'name="abc" state=absent'
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 "name=local description=localyum
baseurl=file:///mnt/ enabled=yes gpgcheck=no"
2 )删除 /etc/yum.repos.d/local.repo 配置⽂件
ansible group01 -m yum_repository -a "name=local state=absent"
11 ansible yum 模块的使用
yum 模块⽤于使⽤ yum 命令来实现软件包的安装与卸载。
1 )安装软件
[root@1 ~] # ansible group01 -m yum -a 'name=ntpdate state=present'
state=latest 表示安装最新版本
ansible group01 -m yum -a 'name=httpd,httpddevel state=latest'
2 )卸载软件
ansible group1 -m yum -a 'name=httpd,httpddevel state=absent'
12 ansible service 模块的使用
service 模块⽤于控制服务的启动 , 关闭 , 开机⾃启动等。
1 )启动服务
# 启动 vsftpd 服务,并设为开机 动启动
ansible group01 -m service -a 'name=vsftpd state=started enabled=on'
2 )关闭服务
# 关闭 filewalld 服务,并设为开机不 动启动
[root@1 ~] # ansible group01 -m service -a 'name=firewalld state=stopped
enabled=false'
13 ansible script 模块的使用
script 模块⽤于在远程机器上执⾏本地脚本。
(此脚本不⽤给执⾏权限 )
[root@1 ~] # vim /tmp/1.sh
[root@1 ~] # ansible group01 -m script -a '/tmp/1.sh'
练习:使⽤ shell 脚本实现在 group01 的被管理机⾥的 mariadb ⾥创建⼀ 个 abc
[root@1 ~] # vim /tmp/2.sh
#!/bin/bash
yum install mariadb-server -y &> /dev/null
systemctl start mariadb
systemctl enable mariadb
mysql << EOF
create database abc;
quit
EOF
[root@1 ~] # ansible group01 -m script -a '/tmp/2.sh'
14 ansible command 模块与 shell 模块的使用
两个模块都是⽤于执⾏ linux 命令的 , 这对于命令熟悉的⼯程师来说,⽤起来⾮常 high
shell 模块与 command 模块差不多( command 模块不能执⾏⼀些类似 $HOME,>,<,| 等符号,但
shell 可以 )
1 )使用 command 模块创建用户
[root@1 ~] # ansible group01 -m command -a 'useradd user1'
[root@1 ~] # ansible group01 -m command -a 'id user1'
10 .0.0.13 | CHANGED | rc = 0 >>
uid = 2000 (user1) gid = 2000 (user1) = 2000 (user1)
10 .0.0.12 | CHANGED | rc = 0 >>
uid = 2000 (user1) gid = 2000 (user1) = 2000 (user1)
other | CHANGED | rc = 0 >>
uid = 2000 (user1) gid = 2000 (user1) = 2000 (user1)
2 )使用 shell 模块使用管道符进行统计查询信息
[root@1 ~] # ansible group01 -m shell -a 'cat /etc/passwd | wc -l'
10 .0.0.13 | CHANGED | rc = 0 >>
22
10 .0.0.12 | CHANGED | rc = 0 >>
22
other | CHANGED | rc = 0 >>
22
3 )使用 shell 模块使用 $HOME 变量
[root@1 ~] # ansible -m shell group01 -a "cd $HOME;pwd"
10 .0.0.13 | CHANGED | rc = 0 >>
/root
other | CHANGED | rc = 0 >>
/root
10 .0.0.12 | CHANGED | rc = 0 >>
/root
4 )使用 command 模块进行关机
[root@1 ~] # ansible group01 -m command -a 'shutdown -h 0'
10 .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 refused
other | FAILED | rc = -1 >>
Failed to connect to the host via ssh : ssh_exchange_identification: read:
Connection reset by peer
10 .0.0.13 | CHANGED | rc = 0 >>
Shutdown scheduled for 2024 -08-16 16 :49:28 CST, use 'shutdown -c' to
cancel.
注意 : shell 模块并不是百分之百任何命令都可以 , ⽐如 vim ll 别名就不可以。
  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值