Ansible可以在Jupyter Notebook上运行:在CentOS6上建立一个可以运行的环境(包含playbook和Vagrantfile)
首先
我试验了中井悦司先生介绍的用 Jupyter Notebook 运行 Ansible 的可行操作步骤,并记录了构建方法。非常感谢中井先生。
请参考中井先生的博客《文学化自动化》以了解Ansible在Jupyter笔记本上的使用情况。
适用场合
当我从中井先生那里第一次听说通过Jupyter执行Ansible时,说实话我并不太明白。(中井先生,非常抱歉…)
然而,最近有一些项目需要将大量的操作步骤转化为Ansible的Playbook,并自动化构建工作等,我的想法也改变了。
在构建过程中,有些步骤需要进行GUI操作,或者必须物理中断,总之就是Playbook无法完全自动化的部分出现了。
对于无法自动化的部分,我原本打算通过操作手册和Jenkins项目来解决,
突然想到了使用Jupyter,尝试了一下发现,它非常方便,不仅可以进行迁移,还可以进行结果的状态报告和共享。
构建环境
在中井的博客中,使用CentOS7上的Docker来运行,但由于种种原因无法使用Docker,所以参考了Dockerfile的内容,
这次在CentOS6上安装了以下内容并进行了构建,将其制作成了一个Vagrant的Box文件,在现场使用了它。
请参考这篇文章以了解关于Vagrant的资讯,其中包括在32位Windows7上运行Ansible等方法的闭环网络环境。
Jupyter本身可以很容易地使用pip install Jupyter进行安装,但是由于CentOS 6默认的Python版本是2.6.6,所以无法正常运行。于是我们通过Pyenv将Python升级到版本2.7.8,然后进行了Jupyter和Ansible的安装。
只需pyenv的安装部分,请参考这篇文章。
構築的主要步骤
我将在已经安装了CentOS 6并想要安装Jupyter和Ansible的系统上,以root用户身份执行以下操作。
1. 安装pyenv
2. 安装python2.7.8
3. 安装Ansible
4. 安装Jupyter notebook
5. 配置Jupyter notebook
6. 打开Jupyter notebook端口(8888)
7. 设置Jupyter notebook密码(pass)并启动
8. 如果使用Vagrant,请设置端口8888的转发配置
剧本和Vagrantfile(使用Ansible provisoner)等。
请参考以下播放书进行具体的环境构建。
gist链接在这里。
使用git克隆 https://gist.github.com/tbuchi888/240df64592302bbee9d823c1c36e0692
---
- include: install_pyenv_without_proxy.yml
- include: install_jupyter_ansible_without_proxy.yml
---
- hosts: all
gather_facts: no
become: yes
vars:
python_ver: 2.7.8
tasks:
- name: which pyenv
shell: source ~/.bash_profile; which pyenv
register: pyenv_install
failed_when: pyenv_install.rc == 0
- debug: var=pyenv_install
- block:
- debug: msg="---------- block start ----------"
- name: yum install with_items
yum:
name: '{{ item.name }}'
state: latest
register: RESULT
with_items:
- name: gcc
- name: gcc-c++
- name: make
- name: git
- name: openssl-devel
- name: bzip2-devel
- name: zlib-devel
- name: readline-devel
- name: sqlite-devel
- name: bzip2
- name: sqlite
- name: patch
- name: results
debug: var=RESULT.results
- name: git clone pyenv , pyenv-virtualenv
git:
repo: '{{ item.repo }}'
dest: '{{ item.dest }}'
register: RESULT
with_items:
- repo: git://github.com/yyuu/pyenv.git
dest: ~/.pyenv
- repo: git://github.com/yyuu/pyenv-virtualenv.git
dest: ~/.pyenv/plugins/pyenv-virtualenv
- name: results
debug: var=RESULT.results
- name: "install pyenv and python ver.{{ python_ver }}"
shell: '{{ item.cmd }}'
register: RESULT
with_items:
- cmd: echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bash_profile
- cmd: echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bash_profile
- cmd: echo ' export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bash_profile
- cmd: echo ' eval "$(pyenv init -)"' >> ~/.bash_profile
- cmd: echo 'fi' >> ~/.bash_profile
- cmd: source ~/.bash_profile; pyenv -v
- cmd: source ~/.bash_profile; python -V
- cmd: source ~/.bash_profile; pyenv install '{{ python_ver }}'
- cmd: source ~/.bash_profile; pyenv global '{{ python_ver }}'
- cmd: source ~/.bash_profile; python -V
- name: results
debug: var=RESULT.results
- debug: msg="---------- block end ----------"
when: pyenv_install.rc == 1
请根据以下的凭据设置Jupyter的密码:jupyter_password: pass,请适时更改。
---
# Setting the destination inventory host
- hosts: all
become: yes
become_method: sudo
become_user: root
# Don't gather hosts facts for performance
gather_facts: no
vars:
jupyter_password: pass
# Setting the task
tasks:
- name: yum install with_items
yum:
name: '{{ item.name }}'
state: latest
with_items:
- name: '*'
- name: "@Development Tools"
- name: epel-release
- name: python-devel
- name: python-pip
- name: lapack-devel
- name: freetype-devel
- name: libpng-devel
- name: libjpeg-turbo-devel
- name: ansible
- name: copy init.sh for jupyter notebook
template:
src: init.sh.j2
dest: /usr/local/bin/init.sh
mode: u+x
- name: command by shell module with_items
shell: '{{item.cmd}}'
register: RESULT
with_items:
- cmd: source ~/.bash_profile; pip install jupyter
- cmd: source ~/.bash_profile; jupyter notebook --generate-config
- cmd: source ~/.bash_profile; echo "c.NotebookApp.ip = '*'" >>/root/.jupyter/jupyter_notebook_config.py && echo "c.NotebookApp.open_browser = False" >>/root/.jupyter/jupyter_notebook_config.py
- cmd: source ~/.bash_profile; nohup /usr/local/bin/init.sh &
- cmd: lokkit -p 8888:tcp
- debug: var=RESULT.results
Jupyter密码设置和用于启动的jinjya2模板的shell
#!/bin/bash
trap 'pkill -f jupyter-notebook; sleep 3; exit 0' EXIT
if [ "${PASSWORD-undef}" = "undef" ]; then
export PASSWORD='{{jupyter_password}}'
fi
if ! grep -qE '^c.NotebookApp.password =' $HOME/.jupyter/jupyter_notebook_config.py; then
HASH=$(python -c "from IPython.lib import passwd; print(passwd('${PASSWORD}'))")
echo "c.NotebookApp.password = u'${HASH}'" >>$HOME/.jupyter/jupyter_notebook_config.py
fi
unset PASSWORD
unset HASH
mkdir -p $HOME/notebook
cd $HOME/notebook
# Comment out the following line.
#ipython -c '%matplotlib' # build font cache for matplotlib
jupyter notebook
对于Vagrant环境,需要通过vagrant up命令启动CentOS6的服务器,并使用Ansible provisoner安装pyenv环境下的Jupyter和Ansible,并且进行Jupyter在8888端口的转发设置。
请使用您本地环境的CentOS6 box文件名替换掉box文件名。特别是在32位机器环境下,请替换为您自己制作的32位版CentOS6的box文件名。
Vagrantfile 的中文译文:流浪文件
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
# Please replace it with a BOX name of CentOS6 for one's own 32bit when your host is 32bit machine.
config.vm.box = "geerlingguy/centos6"
# Add forwarded port to use on Jupyter notebook
config.vm.network "forwarded_port", guest: 8888, host: 8888, host_ip: "127.0.0.1",
auto_correct: true
# Add ansible provisioner
config.vm.provision "ansible" do |ansible|
ansible.playbook = "site.yml"
end
end
用法
– 如果在使用OSX等操作系统的Vagrant环境中可以使用Ansible provisoner的情况下,
– 请准备好上述playbook等资料,然后执行vagrant up命令。
– 如果只有Ansible环境的情况下,
– 请各自准备好主机清单文件,并执行Playbook来操作CentOS6服务器。
ansible-playbook -i 主机清单文件 site.yml
执行上述Playbook的环境
Ansible赋能和Vagrant、Virtualbox主机
– 操作系统:OSX Yosemite
– 虚拟机:版本 5.0.10 r104061
– Vagrant:Vagrant 1.8.1
– Ansible:
$ansible --version
ansible 2.1.0 (devel c600ab81ee) last updated 2016/04/20 11:11:25 (GMT +900)
lib/ansible/modules/core: (detached HEAD 98322a777f) last updated 2016/04/20 11:11:54 (GMT +900)
lib/ansible/modules/extras: (detached HEAD 1aecfc1e19) last updated 2016/04/20 11:11:55 (GMT +900)
config file =
configured module search path = Default w/o overrides
Jupyter和Ansible服务器
– 操作系统:CentOS6.8