Ansibleを使ってMacにgitconfigを設定する
(追記)
Ansible 2.1からgit_configモジュール出た模様。
https://docs.ansible.com/ansible/git_config_module.html
3月になって自分のMacにEl Capitanをクリーンインストールしたので、環境構築をAnsibleを使って次からコマンド一発で大体出来上がるようにしたかったのが当初の予定。。
妥協しながらも、中盤あたりで「shell
でいいじゃん」とか思ったのだけれどもせっかくなのでやってみました。
ただ作るだけだとつまらないのでtemplates
とかansibleの機能をいろいろ試したのでメモ。
GitHub - tomoya-k31/mac-ansible-playbook
gitconfig
$ git config --global user.name "tomoya-k31"
普通?に設定する時は、上記のようなコマンドをTerminalから呼んであげれば、自動で~/
に.gitconfig
ファイルが生成されて
[user] name = tomoya-k31
このように自動で入力されます。(shellで十分だなぁ)
playbookで使用するgitconfigの元となるデータを保持するyamlファイルを定義
--- git_config: user: name: tomoya-k31 email: chacogre@gmail.com color: branch: auto diff: auto interactive: auto status: auto ui: auto core: editor: "'vim -c \"set fenc=utf-8\"'" precomposeunicode: true quotepath: false diff: tool: vimdiff push: default: simple
ansibleでは↑のようなデータは変数として、"{{ git_config.user.name }}"
で取得することが出来ます。
ただ、gitconfigで1つずつ変数を取得してgitconfigにセット..とかは面倒なので、
上記のYamlをtemplate
機能を使ってgit config -- global ...
コマンドの書かれたshellファイルを実行権限をつけた状態で生成して、実行するようにします。
templates
#!/usr/bin/env bash {% for section, values in git_config.iteritems() %} {% for key, value in values.iteritems() %} git config --global {{ section }}.{{ key }} {{ value }} {% endfor %} {% endfor %}
今回はgitconfig.j2
に上記のように記載してます。
git_configから辞書型のイテレートを取得して、2重のfor文
でgit config
コマンドを作っています。
rule
--- - name: generate shell script 'git config --global ...' template: src: gitconfig.j2 dest: ~/write_gitconfig.sh mode: 0744 # 実行権限付与 when: git_config - name: execute shell script shell: ~/write_gitconfig.sh - name: remove shell script file: path: ~/write_gitconfig.sh state: absent
流れとしては、
- templateから
~/write_gitconfig.sh
を作成 ~/write_gitconfig.sh
の実行~/write_gitconfig.sh
ファイルの削除
実行
$ ansible-playbook -i hosts local_mac.yml PLAY *************************************************************************** TASK [setup_git : generate shell script 'git config --global ...'] ************* changed: [localhost] TASK [setup_git : execute shell script] **************************************** changed: [localhost] TASK [setup_git : remove shell script] ***************************************** changed: [localhost] PLAY RECAP ********************************************************************* localhost : ok=3 changed=3 unreachable=0 failed=0
以上でした。
余談
最近、ローカル環境にHackフォント入れました。 Powerline対応版もあって結構良さげだったのでしばらく使っていきます。