Ansible主要是通过SSH协议进行机器的管理
安装完成之后,不需要启动或者运行一个后台进程,或者添加一个数据可,只要在一台电脑上安装好,就可以通过一台机器管理一组远程机器,而在远程机器上不需要安装任何软件
一、ansible的安装
1)yum安装
yum install -y ansible
2)pip安装
pip是一个安装和管理python软件包的工具
pip install -y ansible
3)源码安装
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
二、三台主机之间实现免密码登录 ——【一主二辅】
管理主机:192.168.3.6
控制主机:192.168.3.69 192.168.3.70
生成公钥和私钥:ssh-keygen -P ''
将/root/.ssh下的id_rsa.pub文件拷贝到另外的主机
注:ssh-copy-id命令会自动将id_rsa.pub文件的内容追加到远程主机root用户下.ssh/authorized_keys文件中。
ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.3.69
三、ansible配置文件
hosts文件是用来指定被ansible管理的文件
roles是用来指定每一个角色
ansible.cfg核心配置文件
配置文件目录:/etc/ansible
[root@localhost ansible]# ll
total 24
-rw-r--r-- 1 root root 19336 Apr 18 03:46 ansible.cfg
-rw-r--r-- 1 root root 1048 Apr 18 03:53 hosts
drwxr-xr-x 2 root root 6 Mar 27 17:30 roles
1)编辑ansible.cfg文件:
#禁用每次执行ansible命令检查ssh key host
host_key_checking = False
#开启日志记录
log_path = /var/log/ansible.log
#ansible连接加速装置
accelerate_port = 10000
accelerate_multi_key = yes
2)编辑hosts文件
#添加控制主机并分组
[webservers]
192.168.3.69
[dataserver]
192.168.3.70
3)模块介绍
【command模块】
在管理主机上执行shell命令的模块:-a 运行命令
ansible web -a "ifconfig"
【shell模块】
shell模块支持使用管道,而command不支持使用
ansible app -m shell -a "echo '123'| passwd --stdin jk"
特别的:-m 指明所使用的模块 -a 运行相应的命令
【copy模块】
将ansible上的文件复制到指定控制主机上
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
当然在复制过程中也可以指定 owner=,group= ,mode= , 例如:
ansible all -m copy -a "src=/etc/fstab dest=/tmp owner=yhy group=yhy mode=644"
同样的也可以将内容复制过去。例如
ansible all -m copy -a "content='hello i will come back ' dest=/tmp/hello owner=yhy group=yhy mode=644"
特别的:all 表示所有控制主机
【yum模块】
基于yum管理程序包
#安装httpd程序包,latest表示安装的包是最新的
ansible app -m yum -a "name=httpd state=latest"
#卸载httpd程序包
ansible app -m yum -a "name=http* state=absent"
【service模块】
管理被监控的主机
#让服务启动与关闭
ansible app -m service -a "name=httpd state=started"
ansible app -m service -a "name=httpd state=stoped"
4)YAML文件
四、使用ansible部署LAMP环境
前提:管理主机上已经配置好关于HTTP、PHP、MySQL主配置文件的相关配置参数
1)创建YAML文件 —— lamp.yml(剧本)
- hosts: webservers
remote_user: root
tasks:
- name: Install Apache Httpd
yum: name={{ item }} state=present disable_gpg_check=yes
with_items:
- httpd
- php
- php-mysql
- name: Install Configuration File
template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: Start Httpd Service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
- hosts: dataserver
remote_user: root
tasks:
- name: Install MySQL Server
yum: name=mariadb-server state=present disable_gpg_check=yes
- name: Install Configuration File
template: src=/etc/my.cnf dest=/etc/my.cnf
notify:
- restart MySQL
- name: Start MySQL Server
service: name=mariadb state=started
handlers:
- name: restart MySQL
service: name=mariadb state=restarted
2)运行
ansible-playbook lamp.yml
此时已经在控制主机上webserver上安装好http和php,在dataserver上安装好MySQL
3)在控制主机dataserver上安装python连接MySQL的驱动
ansible dataserver -m yum -a "name=MySQL-python state=present disable_gpg_check=yes"
4)在控制主机dataserver上创建MySQL的用户
ansible dataserver -m mysql_user -a "name=test password=12345 host=% state=present"
特别的:因为PHP和MySQL分别安装在不同的主机上面,所以要保证数据库在连接的过程中外网能够进行访问,所以在创建用户的过程中必须给用户授权来保证外网能够连接
5)测试PHP连接MySQL成功
在本地创建index.php页面
PHP连接MySQL的测试代码
<?php
$link = mysql_connect('192.168.3.70', 'test', '12345');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
phpinfo()
?>
ansible webserver -m copy -a "src=/root/index.php dest=/var/www/html/index.php"
在浏览器上进行访问,看是否连接成功
#PHP实现图片循环
<?php
header('Content-Type: image/jpeg');
$data = file_get_contents('/images/1.jpg');
echo $data;
?>
参考文档:
https://www.linuxidc.com/Linux/2016-04/130025.htm https://zhidao.baidu.com/question/559629805.html
MySQL添加新用户、为用户创建数据库、为新用户分配权限:https://blog.csdn.net/piaocoder/article/details/53704126
https://blog.csdn.net/xyang81/article/details/51568227 https://linux.cn/article-4215-2.html