使用ansible编译安装运维工具tmux

实验系统:CentOS 6.6_x86_64

实验前提:提前准备好编译环境,防火墙和selinux都关闭

软件介绍:tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权。使用它最直观的好处就是,通过一个终端登录远程主机并运行tmux后,在其中可以开启多个控制台而无需再“浪费”多余的终端来连接这台远程主机;当然其功能远不止于此。

软件下载:http://pan.baidu.com/s/1c0i9kf2

一、常规安装

  1.安装tmux所需要的依赖:

wget https://sourceforge.net/projects/levent/files/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
tar xf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure
make && make install
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

  2.安装tmux软件包:

wget http://iweb.dl.sourceforge.net/project/tmux/tmux/tmux-2.0/tmux-2.0.tar.gz
tar xf tmux-2.0.tar.gz
cd tmux-2.0
./configure --prefix=/usr/local/tmux
make && make install

  3.导出二进制文件:

vim /etc/profile.d/tmux.sh
---------------------------------------->
PATH=$PATH:/usr/local/tmux/bin
export PATH
<----------------------------------------
. /etc/profile.d/tmux.sh

  4.导出man手册:

vim /etc/man.config
--------------------------------------------->
MANPATH /usr/local/tmux/share/man        //增加一行

  5.编辑配置文件:

vim ~/.tmux.conf
------------------------------------------>
set -g prefix C-a                                     //设置前缀命令为crtl+a
unbind C-b                                            //解除ctrl+b的绑定
setw -g mode-keys vi                                  //copy-mode将快捷键设置为vi模式
set -g default-terminal "screen-256color"             //设置终端颜色为256色
set -g status-utf8 on                                 //开启状态栏的uft-8支持
set-window-option -g mode-mouse on                    //开启滚屏

   6.常用快捷键:

    

    

    

  至此,tmux安装完毕了,下面咱们做个拓展实验,使用ansible安装tmux!

二、拓展实验

   1.安装ansible并创建yaml文件:

yum -y install ansible
mkdir -pv /root/ansible.roles/roles     //创建工作目录
cd /root/ansible.roles
vim tmux.yaml
-------------------------------------------->
- name: install tmux
  remote_user: root                    //运行用户
  hosts: tmux                          //运行这个剧本的主机,后面有定义
  roles:
  - tmux                               //规则名称,要与后面创建的文件夹名称相同

   2.放入文件:

cd /root/ansible.roles/roles
mkdir -pv tmux/{files,handlers,tasks}                                         //创建与规则同名的文件夹
cp /root/tmux-2.0.tar.gz /root/libevent-2.0.22-stable.tar.gz tmux/files/      //将安装包放入
cp /root/.tmux.conf tmux/files/                                               //将配置文件放入

  3.编写主yaml文件:

vim /root/ansible.roles/roles/tmux/tasks/main.yaml
------------------------------------------------------>
- name: copy libevent package
  copy: src=libevent-2.0.22-stable.tar.gz dest=/root
- name: copy tmux package
  copy: src=tmux-2.0.tar.gz dest=/root
- name: copy conf
  copy: src=.tmux.conf dest=/root
- name: run script
  script: tmux.sh

   4.编写tmux.sh脚本:

vim /root/ansible.roles/roles/tmux/files/tmux.sh
---------------------------------------------------------------->
#!/bin/bash
#

# Install libevent
cd && cd
tar xf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure
make && make install
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5

# Install tmux
cd && cd
tar xf tmux-2.0.tar.gz
cd tmux-2.0
./configure --prefix=/usr/local/tmux
make && make install

# Extra Operation
touch /etc/profile.d/tmux.sh 
echo 'PATH=$PATH:/usr/local/tmux/bin' > /etc/profile.d/tmux.sh
echo 'export PATH' >> /etc/profile.d/tmux.sh
echo 'MANPATH /usr/local/tmux/share/man' >> /etc/man.config
. /etc/profile.d/tmux.sh
<----------------------------------------------------------------
chmod +x /root/ansible.roles/roles/tmux/files/tmux.sh

  5.加入要安装的主机:

vim /etc/ansible/hosts
---------------------------------------------->
[tmux]                                       //对应tmux.yaml文件里的名称
192.168.19.76 ansible_ssh_pass=password      //主机IP+登录密码

  6.尝试使用:

cd /root/ansible.roles/
ansible-playbook tmux.yaml

    

  至此,实验全部完成。使用中发现/etc/profile.d/tmux.sh这个文件不能被正常source,所以可能需要手动执行一下 . /etc/profile.d/tmux.sh 。由于时间紧迫,所以过程还不是很完善,脚本也并不严谨,没有一些条件判断等等,而且安装主机必须要有编译环境。大家如果有需要可以自行扩展修改,我已经把ansible文件夹上传至共享,大家可以随意下载使用。最后,感谢大家的收看,谢谢!如有问题,请联系QQ:82800452.

posted @ 2015-09-18 10:48  阿姜  阅读(2837)  评论(1编辑  收藏  举报