Record the flow from Mac to setting up Python environment using Ansible for Vagrant virtual machine.
Virtual machine preparation
Move to the folder (AnsTest) where you want to put the virtual machine configuration file.
vagrant init ubuntu/trusty64
A file called `` `Vagrantfile``` will be created, so open it with your favorite editor.
Uncomment `` `config.vm.network" private_network ", ip:" 192.168.33.10 "```.
Create a Python virtual environment on the host
``` mkvirtualenv ans --no-site-packages --python = (Python path)` ``
Specify Python 2.x series. It seems that Python 3.x series will be supported in the future.
Install Ansible
pip install ansible
Creating and deploying inventory files
Place it in `` `/ etc / ansiblewith the name
hosts``` (optional).
Write the following content. In [], enter the name to be specified as host in the playbook later, and the IP address in the line commented out in the Vagrantfile.
[AnsTest]
192.168.33.10
Create a playbook
The contents will be explained later.
- hosts: AnsTest
become: yes
user: vagrant
tasks:
- name: Install essentials
apt: pkg={{ item }} update_cache=yes state=present
with_items:
- vim
- git
- build-essential
- libssl-dev
- zlib1g-dev
- libbz2-dev
- libreadline-dev
- libsqlite3-dev
- wget
- curl
- llvm
- libncurses5-dev
- libncursesw5-dev
- xz-utils
- name: Clone pyenv
become: yes
become_user: vagrant
git:
repo: https://github.com/yyuu/pyenv.git
dest: /home/vagrant/.pyenv
- name: Edit bash
become: yes
become_user: vagrant
blockinfile:
create: yes
dest: /home/vagrant/.bash_profile
content: |
# pyenv
export PYENV_ROOT=/home/vagrant/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
- name: Install Pythons
become: yes
become_user: vagrant
shell: . /home/vagrant/.bash_profile && pyenv install -s 2.7.11 && pyenv install -s 3.5.1
ansible-playbook provision_vagrant.yml -k
-k
option. Otherwise, you may get an authentication failure error. The default is `` `vagrant```.There are many other good articles on basic writing and how to use modules, so I'll only write about where I'm addicted.
- name:Name of processing
(Module name):(...)
(Module execution details):(...)
- name:Name of processing
(Module name):(...)
(Module execution details):(...)
(...)
However, in some cases, `attribute = value`
is written next to the module name, {{variable}} is told or not double quoted, and the grammar is still unknown.
Run-time user
At first, I did not specify the user and executed it only with `become: yes``` on the second line, but later I made an SSH connection with
vagrant ssh``` and ``
pyenv uninstall` `` When I tried, I couldn't delete it because I didn't have permission.
About environment variables
This took the most time. At first, I wrote around the path to .bashrc
, but when I write it with `blockinfile``` etc. and go to the next task, [Start in another shell](http: // qiita.com/FGtatsuro/items/2366c93131c47aef8dfe) ,
`pyenv install x.x.x``` will result in an error when the settings written in .bashrc are not reflected.
Then, I thought that I should read it on the spot, so I can not force it to read with `. /home/vagrant/.bashrc```, and as a result of trial and error, the writing destination is
.bash_profile``` It came to work by doing.
Half of my Ansible history was fighting around this environment variable ... this I took it to the point where it was forced to move. For the difference between .bashrc, .bash_profile, etc., here may be helpful.
Recommended Posts