上一篇介绍了ansible基础知识,做了ansible.cfg及hosts文件配置。
本实验在上一个实验的基础上进行的。
上一篇链接:https://blog.csdn.net/dghfttgv/article/details/104800183
目录
一、Ansible-doc用法简介
- 1、Asible-doc的使用场景
- 2、Ansible-doc命令的使用方法
二、远程下载httpd到server2上
(一)、远程安装httpd服务
- 1、查看远程下载的用法
- 2、远程下载httpd到server2上
- 3、删除远程主机上的应用软件
- 4、查看服务部署过程的使用方法
(二)、用Ansible-doc语法启动httpd服务
- 1、service模块
- 2、开启httpd服务
(三)、远程配置httpd的发布目录
- 1、创建发布文件的内容
- 2、复制index.html到server2的/var/www/html/目录下
- 3、设置httpd服务开机自启
(四)、设置防火墙规则
- 1、查看防火墙规则的书写格式
- 2、启动防火墙服务
- 3、设置防火墙规则
三、远程创建文件
- 1、远程创建文件
- 2、删除创建的testdir文件信息
四、修改服务httpd服务的端口
- 1、修改服务的端口:
- 2、将配置好的httpd配置文件复制到server2主机
- 3、重启httpd服务使配置文件生效
- 4、测试
五、远程创建用户
- 1、远程创建主机
- 2、远程创建用户登录失败的解决方法
- 3、测试
- 4、登录远程创建的用户
六、远程安装、登录数据库
(一)、远程安装数据库
- 1、远程安装mariadb
- 2、启动服务mariadb
- 3、测试
- 4、创建数据库用户
(二)、普通用户远程登录数据库
- 1、远程操作数据库的用户身份权限设置
- 2、普通用户远程登录数据库的方法
- 3、远程登录数据库的方法:
- (1)安装mysql软件
- (2)登录数据库
一、Ansible-doc用法简介
- 1、Asible-doc的使用场景
- 2、Ansible-doc命令的使用方法
Ansible提供两种方式取完成任务,一是ad-hoc 命令 ,一是Ansible playbook,在学习playbook 之后,你才能体会到Ansible真正的强大
Ad-hoc 更注重于解决一些简单或者平时工作中临时遇到的任务相当于Linux命令下执行的Shell命令 后者更适合解决复杂或需要固化下来的任务,相当Linux系统中的Shell Scripts。
使用场景:
关闭所有不必要的服务器
临时更新Apache或nginx配置文件
Ansible和Ansible-playbok默认会fork 5 个线程 执行命令 ,如果同时操作的主机数比较多的话,可以调整到一个最大值。
Asible为我们提供了便捷的选项,-f指定线程数;
例如:ansible webserver -m -f 3
ansible 有许多的模块 默认是command,也就是命令模块我们可以通过 -m 选现来指定不同的模块
ansible模块非常之多。Asible也提供了类似于man功能的help说明工具 ansible-doc
正是学习Asible模块之前,有必要先了解ansible-doc用法
ansible的模块非常只多,Ansible也提供了类似与man功能的help说明工具ansible-doc。
2、Ansible-doc命令的使用方法
- ansible-doc -l #显示所有可用模块
- ansible-doc yum #获取yum模块使用帮助
获取yum模块帮助
#ansible-doc yum
[ansible@server1 ~]$ ansible-doc -l
二、远程下载httpd到server2上
(一)、远程安装httpd服务
- 1、查看远程下载的用法
- 2、远程下载httpd到server2上
- 3、删除远程主机上的应用软件
- 4、查看服务部署过程的使用方法
(二)、用Ansible-doc语法启动httpd服务
- 1、service模块
- 2、开启httpd服务
(三)、远程配置httpd的发布目录
- 1、创建发布文件的内容
- 2、复制index.html到server2的/var/www/html/目录下
- 3、设置httpd服务开机自启
(四)、设置防火墙规则
- 1、查看防火墙规则的书写格式
- 2、启动防火墙服务
- 3、设置防火墙规则
1、查看远程下载的用法
[ansible@server1 ~]$ ansible-doc yum ##查ansible-doc的用法
1.1、查看httpd部署的相关命令
2、远程下载httpd到server2上
[ansible@server1 ~]$ ansible test -m yum -a "name=httpd state=present" ##远程下载httpd到server2上
在server2上查看部署信息
部署之后查看:
3、删除远程主机上的应用软件
[ansible@server1 ~]$ ansible test -m yum -a "name=httpd state=absent" ##删除httpd服务
4、查看服务部署过程的使用方法(在部署命令后加 -vvv,v的个数越多消息越详细)
[ansible@server1 ~]$ ansible test -m yum -a "name=httpd state=present" -vvv ##后线加v多加几个能看清楚过程
(二)、用Ansible-doc语法启动httpd服务
- 1、service模块
- 2、开启httpd服务
安装完httpd服务之后要启动服务
1、service模块
[ansible@server1 ~]$ ansible-doc service ##查看service的配置方法
2、开启httpd服务
[ansible@server1 ~]$ ansible test -m service -a "name=httpd state=started"
设置开启器自启
2.2、测试
访问servre2
[ansible@server1 ~]$ curl server2
(开启服务成功)
(三)、远程配置httpd的发布目录
- 1、创建发布文件的内容
- 2、复制index.html到server2的/var/www/html/目录下
- 3、设置httpd服务开机自启
1、创建发布文件的内容
[ansible@server1 ~]$ vim index.html ##创建index.html文件
[ansible@server1 ~]$
[ansible@server1 ~]$ cat index.html
hello server2
2、复制index.html到server2的/var/www/html/目录下
输入命令:
[ansible@server1 ~]$ ansible test -m copy -a "src=index.html dest=/var/www/html/index.html"
测试:
[ansible@server1 ~]$ curl server2
hello server2
3、设置httpd服务开机自启
3.1、输入开机自启命令
[ansible@server1 ~]$ ansible test -m service -a "name=httpd enabled=yes" ##设置为开机自启
3.2、测试
在server2上查看httpd服务的状态
[root@server2 ~]# systemctl is-enabled httpd
enabled
[root@server2 ~]#
(四)、设置防火墙规则
- 1、查看防火墙规则的书写格式
- 2、启动防火墙服务
- 3、设置防火墙规则
1、查看防火墙规则的书写格式
#ansible-doc firewalld
设置防火墙的规则:
[ansible@server1 ~]$ ansible-doc firewalld ##查看防火墙的配置规则
2、启动防火墙服务
[ansible@server1 ~]$ ansible test -m service -a "name=firewalld state=started" ##开启防火墙服务
[ansible@server1 ~]$ ansible test -m service -a "name=firewalld enabled=yes" ##开机自启
2.2、在server2上查看火墙的状态
[root@server2 ~]# systemctl is-enabled firewalld
enabled
[root@server2 ~]#
3、设置防火墙规则
[ansible@server1 ~]$ ansible test -m firewalld -a "service=http permanent=yes state=enabled immediate=yes" ##设置防火墙规则
3.2、测试
[ansible@server1 ~]$ curl server2
hello server2
三、远程创建文件
- 1、远程创建文件
- 2、删除创建的testdir文件信息
1、远程创建文件
[ansible@server1 ~]$ ansible test -m file -a "path=/var/www/html/index.html mode=755" ##修改文件的属性与权限
[ansible@server1 ~]$ ansible test -m file -a "path=/tmp/testdir mode=755 state=directory" ##修改文件的权限
1.1、测试
在server2上查看创建的文件的属性
[root@server2 ~]# ll /tmp/
2、删除创建的testdir文件信息
[ansible@server1 ~]$ ansible test -m file -a "path=/tmp/testdir state=absent"
server2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/testdir",
"state": "absent"
}
[ansible@server1 ~]$
2.2、测试
在server2上查看:
[root@server2 ~]# ll /tmp/
(testdir文件被删)
四、修改服务httpd服务的端口
- 1、修改服务的端口:
- 2、将配置好的httpd配置文件复制到server2主机
- 3、重启httpd服务使配置文件生效
- 4、测试
1、修改服务的端口:
修改httpd服务的配置文件信息
1.1、将httpd服务的配置文件复制到当前目录下
[ansible@server1 ~]$ scp server2:/etc/httpd/conf/httpd.conf .
httpd.conf 100% 11KB 8.5MB/s 00:00
[ansible@server1 ~]$
1.2、将端口改为8080
[ansible@server1 ~]$ vim httpd.conf ##修改配置文件信息
Listen 8080
2、将配置好的httpd配置文件复制到server2主机
[ansible@server1 ~]$ ansible test -m copy -a "src=httpd.conf dest=/etc/httpd/conf/httpd.conf"
3、重启httpd服务使配置文件生效
[ansible@server1 ~]$ ansible test -m service -a "name=httpd state=restarted"
4、测试
在server2上安装net-tools服务方便查看端口信息
[root@server2 ~]# yum install net-tools -y
[root@server2 ~]# netstat -antlp
tcp6 0 0 :::8080 :::* LISTEN 8730/httpd ##8080端口已经生效
- 在server1上访问server2:8080 (访问别拒绝)
解决:
修改火墙策略允许8080端口访问
[ansible@server1 ~]$ ansible test -m firewalld -a "port=8080/tcp permanent=yes state=enabled immediate=yes"
测试:
在server1上访问server2:8080
[ansible@server1 ~]$ curl server2:8080
hello server2
[ansible@server1 ~]$
五、远程创建用户
- 1、远程创建主机
- 2、远程创建用户登录失败的解决方法
- 3、测试
- 4、登录远程创建的用户
1、远程创建主机
远程登录(登录失败!!)
[ansible@server1 ~]$ ansible test -m user -a "name=user1 password=123" ##远程创建主机
[ansible@server1 ~]$ ssh user1@server2 ##登录测试
user1@server2's password:
Permission denied, please try again.
user1@server2's password:
Permission denied, please try again.
user1@server2's password:
2、远程创建用户登录失败的解决方法
解决方法:
[ansible@server1 ~]$ ansible test -m user -a "name=user1 password={{'westos' | password_hash('sha512', 'mysecretsalt')}}"
3、测试
在server2上查看用户是否已经建立
[root@server2 ~]# cat /etc/passwd
user1:x:1002:1002::/home/user1:/bin/bash
4、登录远程创建的用户
server1上:
[ansible@server1 ~]$ ssh user1@server2
user1@server2's password:(输入westos)
Last failed login: Sat Mar 7 09:15:18 EST 2020 from server1 on ssh:notty
There were 2 failed login attempts since the last successful login.登录成功!!!!
六、远程安装、登录数据库
(一)、远程安装数据库
- 1、远程安装mariadb
- 2、启动服务mariadb
- 3、测试
- 4、创建数据库用户
(二)、普通用户远程登录数据库
- 1、远程操作数据库的用户身份权限设置
- 2、普通用户远程登录数据库的方法
- 3、远程登录数据库的方法:
- (1)安装mysql软件
- (2)登录数据库
1、远程安装mariadb
[ansible@server1 ~]$ ansible test -m yum -a "name=mariadb-server state=present"
在server2上查看安装进程
2、启动服务mariadb
[ansible@server1 ~]$ ansible test -m service -a "name=mariadb state=started"
2.2、查看报错信息
[ansible@server1 ~]$ ansible test -m mysql_db -a "name=westos state=present" ##查看提示安装的软件
server2 | FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required." ##提示缺少软件包
}
[ansible@server1 ~]$
2.3、安装提示的软件
解决:
[ansible@server1 ~]$ ansible test -m mysql_db -a "name=westos state=present"
server2 | CHANGED => {
"ansible_facts":
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"db": "westos",
"db_list": [
"westos"
3、测试
登录数据库
在server2上登录mysql
[root@server2 ~]# mysql ##登录MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql
MariaDB [mysql]> select * from user; ##查看创建的用户
4、创建数据库用户
4.1、查看数据库的启动策略
[ansible@server1 ~]$ ansible-doc mysql_db
4.2、创建数据库用户
[ansible@server1 ~]$ ansible test -m mysql_user -a "name=yb host='%' password=westos state=present priv='westos.*:ALL'" ##创建用户;百分号表示远程登录
4.3、测试:
在server2上:
4.3.1、登录数据库
MariaDB [mysql]> select * from user;
查看新创建的用户
(二)、普通用户远程登录数据库
- 1、远程操作数据库的用户身份权限设置
- 2、普通用户远程登录数据库的方法
- 3、远程登录数据库的方法:
- 安装mysql软件
- 登录数据库
1、远程操作数据库的用户身份权限设置
只有超级用户才能登录数据库
1.1、此时登录默认的用户是超级用户(之前在.ansible.cfg文件中设置的)
把超级用户的权限给关掉
[ansible@server1 ~]$ vim .ansible.cfg ##进入ansible主配置文件中修改文件的内容
1.2、查看
1.2.1、关掉权限之后再次登录系统 系统会报错没有权限
[ansible@server1 ~]$ ansible test -m mysql_user -a "name=yb host='%' password=westos state=present priv='westos.*:ALL'"
查看会提示没有权限 !!
2、普通用户远程查看数据库的方法:
加login_user=root ##默认是超级用户登录
[ansible@server1 ~]$ ansible test -m mysql_user -a "login_user=root name=yb host='%' password=westos state=present priv='westos.*:ALL'"
(远程登录成功!!!)
3、远程登录数据库的方法:
server1上:
- 安装mysql软件
- 登录数据库
3.1、安装mysql软件
yum install -y mysql ##安装,数据库软件
[root@server1 ~]# mysql -h server2 -u yb -pwestos ##登录数据库
ERROR 2003 (HY000): Can't connect to MySQL server on 'server2' (113) ##连接失败 因为有防火墙
[root@server1 ~]#
3.2、设置防火墙规则
[ansible@server1 ~]$ ansible test -m firewalld -a "port=3306/tcp permanent=yes state=enabled immediate=yes"
3.3、测试:
远程登录数据库
[ansible@server1 ~]$ mysql -h server2 -u yb -p ##用户远程登录server2数据库
(登录成功!!!!)