一、使用说明:
之前一直用的saltstack自动化管理工具,需要维护客户端,而去批量执行时网络延迟也是个很头疼的问题,后来接触到了ansible这个工具,不需要安装客户端,走的ssh的加密协议,比较方便。今天这里说一下使用ansible自动化安装nginx的操作。
二、实验环境:
3台服务器(centos 6.5 final版本):
1
2
3
192.168.1.193(ansible服务端)
192.168.1.194(安装nignx)
192.168.1.195(安装nignx)
三、安装配置:
在192.168.1.193上面安装ansible,可以直接用yum直接安装,安装之前,需要给yum添加个epel的源。
安装好epel之后,就可以直接用yum安装ansible了
装好ansible,其它都不需要修改,只需要将被控服务器添加到hosts文件里面就好。
1
2
3
4
5
6
vim /etc/ansible/hosts
内容为:
[testhosts]
192.168.1.194
192.168.1.195
因为ansible通过ssh来控制被控服务器,所以想要在控制服务器时不需要输入密码,那么就需要把192.168.1.193的公钥放置到被控服务器上面。操作方法如下:
先在192.168.1.193上面生成密钥:
1
ssh-keygen -t rsa -P ''
一直enter键就行了,将生成一个公钥和私钥。-P '' 表示空密码。
然后将公钥copy至被控服务器,并写入authorized_keys,这可以用一个命令一次性搞定:
ok,到这里就可以用ansible测试一下了。
1
2
3
4
5
6
7
8
9
10
11
ansible testhosts -m ping
结果为:
192.168.1.194 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.1.195 | SUCCESS => {
"changed": false,
"ping": "pong"
}
表示ansible可以无需输入密码控制194和195两台服务器了。
下面书写playbook来自动化安装nginx到194和195两台服务器上:
1
2
[iyunv@localhost playbook]# ls
base-install.yml nginx-install-common.yml
base-install.yml内容为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---
#check or madir in remote server.
- name: check if the dir software existed, if not existed, then mkdir it.
file:
path: "{{ base_dir }}/auto_deploy/software"
owner: root
group: root
mode: 0755
state: directory
- name: check if the dir config_file existed, if not existed, then mkdir it.
file:
path: "{{ base_dir }}/auto_deploy/config_file"
owner: root
group: root
mode: 0755
state: directory
- name: check if the dir install_log existed, if not existed, then mkdir it.
file:
path: "{{ base_dir }}/auto_deploy/install_log"
owner: root
group: root
mode: 0755
state: directory
nginx-install-common.yml内容为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
- hosts: 192.168.1.194 192.168.1.195
gather_facts: no
remote_user: root
#become_method: sudo
#become_user: root
vars:
- base_dir: "/home/workspace"
- install_dir: "/usr/local/rktsapps"
tasks:
- include: base-install.yml
- name: copy nginx dir to remote servers.
copy:
src=\'#\'" /ansible_home/auto_deploy/software/nginx"
dest: "{{ base_dir }}/auto_deploy/software/"
- name: copy nginx conf to remote servers.
copy:
src=\'#\'" /ansible_home/auto_deploy/config_file/nginx"
dest: "{{ base_dir }}/auto_deploy/config_file/"
- name: copy install script to remote servers.
copy:
src=\'#\'" /ansible_home/auto_deploy/auto_install.sh"
dest: "{{ base_dir }}/auto_deploy/"
- name: test if nginx has been installed in remote server.
shell: ls "{{ install_dir }}/nginx/sbin/nginx"
ignore_errors: True
register: result
- name: execute shell script to install nginx in remote servers.
shell: sh auto_install.sh nginx
ignore_errors: True
args:
chdir: "{{ base_dir }}/auto_deploy/"
when: result|failed
- name: delete auto_install.sh .
file:
path: "{{ base_dir }}/auto_deploy/auto_install.sh"
state: absent
然后执行
1
ansible-playbook nginx-install-common.yml --syntax-check
这里只是检查一下yml的语法是否正确,并没有正确的执行。
检查无误,再执行:
1
ansible-playbook nginx-install-common.yml
就可以执行nginx的安装操作了。大功告成。
四、注意说明:
这里主要是为了让大家熟悉ansible的安装,初始化配置,已经playbook的语法
ansible在这里做的主要是建立目录,从193上面复制一些nginx安装文件,配置文件到194和195上面
主要的安装过程在auto_instal.sh脚本里面,也就是新建用户,编译安装,添加配置文件的一些过程。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com