Qiang Blog

Just another zhangjingqiang's blog.

How to bulk set multiple servers use different colors with ansible?

2017年1月Gitlab的数据库误删除事件使全世界对服务器的安全重视起来,把不同的服务器设置成不同的颜色背景是一个较有效的方法。下面使用 Ansible 设置 Tmux 的 powerline 区分不同环境的服务器。

Directory

.
├── README.md
└── provisioning
    ├── files
    │   └── .zshrc.yml
    ├── inventory
    ├── playbook.yml
    ├── tasks
    │   ├── tmux.yml
    │   └── zsh.yml
    └── templates
        └── .tmux.conf.j2

How to use?

$ cd provisioning
$ ansible-playbook playbook.yml -i inventory

Source

provisioning/files/.zshrc

if [ "$TMUX" = ""   ]; then tmux; fi

provisioning/templates/.tmux.conf.j2

source-file "/home/{{username}}/.tmux-themepack/powerline/block/{{color}}.tmuxtheme"

provisioning/tasks/tmux.yml

---
- name: Install the latest version of Tmux
  yum: name=tmux state=latest

- name: Install tmux-thmepack
  git: repo=https://github.com/jimeh/tmux-themepack
       dest=/home/{{username}}/.tmux-themepack

- name: Copy .tmux.conf file to servers
  template:
    src: templates/.tmux.conf.j2
    dest: /home/{{username}}/.tmux.conf

provisioning/tasks/zsh.yml

---
- name: Install the latest version of Zsh
  yum: name=zsh state=latest

- name: Copy .zshrc file to servers
  copy:
    src: files/.zshrc
    dest: /home/{{username}}/.zshrc

- name: Start zsh shell
  user: name={{username}} shell=/bin/zsh

provisioning/playbook.yml

---
- hosts: all
  become: yes
  vars_prompt:
    name: "username"
    prompt: "Enter username"
    private: no

  tasks:
    - include: tasks/tmux.yml
    - include: tasks/zsh.yml

provisioning/inventory

[server1]
app1.server1
app2.server1

[server1:vars]
color=blue

[server2]
app1.server2
app2.server2

[server2:vars]
color=orange

[server3]
app1.server3
app2.server3

[server3:vars]
color=red

[servers:children]
server1
server2
server3

ansible tmux zsh