{
    "componentChunkName": "component---src-templates-blog-post-jsx",
    "path": "/post/refactored-ansible-settings-to-setup-golang/",
    "result": {"data":{"site":{"siteMetadata":{"title":"WEB EGG","author":"Leko - CTO at Yuimedi"}},"markdownRemark":{"id":"bca617d4-9937-588c-850b-15cbdf6104dc","excerpt":"Hamee advent calendar13日目 Ansible advent calendarの13日目 の投稿です。 まだGoもAnsibleも良くわかってない頃に作った、Goの環境を整えるAnsibleの設定をリファクタリングしました。 やってる事はごく当たり前なんですが、冪等性を担保し、changed…","html":"<ul>\n<li><a href=\"http://qiita.com/advent-calendar/2015/hamee\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Hamee advent calendar</a>13日目</li>\n<li><a href=\"http://qiita.com/advent-calendar/2015/ansible\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ansible advent calendar</a>の13日目</li>\n</ul>\n<p>の投稿です。</p>\n<p>まだGoもAnsibleも良くわかってない頃に作った、Goの環境を整えるAnsibleの設定をリファクタリングしました。</p>\n<p>やってる事はごく当たり前なんですが、冪等性を担保し、changedが出ないことによって何回でもいつでも実行できる環境にしたので、その過程をメモします。</p>\n<!--more-->\n<h2 id=\"まえおき\" style=\"position:relative;\"><a href=\"#%E3%81%BE%E3%81%88%E3%81%8A%E3%81%8D\" aria-label=\"まえおき permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>まえおき</h2>\n<p><a href=\"https://github.com/Leko/godemo/blob/91d4c1b439e9546ff14f6aeae44edee2bc28d262/provisioning/playbook.yml\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">リファクタリング前</a>と <a href=\"https://github.com/Leko/godemo/blob/e0d9ca401755e0c4b02ac4a07f4f9bf8632fbaa4/provisioning/playbook.yml\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">リファクタリング後</a>はそれぞれリンクをご参照下さい</p>\n<h2 id=\"goのバージョンを最新に\" style=\"position:relative;\"><a href=\"#go%E3%81%AE%E3%83%90%E3%83%BC%E3%82%B8%E3%83%A7%E3%83%B3%E3%82%92%E6%9C%80%E6%96%B0%E3%81%AB\" aria-label=\"goのバージョンを最新に permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Goのバージョンを最新に</h2>\n<p>Goのバージョンが1.4で古かったので1.5に上げました。<br>\nちなみに1.4と1.5は<a href=\"https://golang.org/doc/go1.5\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">こんな感じ</a>に違うそうです。</p>\n<p>URL内にバージョン番号をハードコードしていたのでvarsの中に移して、バージョン番号の変更漏れを起こさないようにしました。(<a href=\"https://github.com/Leko/godemo/commit/1af93346995a444a4992b96e7086c5b8def03701?diff=unified\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">差分</a>)<br>\n以後はURLに変更がなければ<a href=\"https://github.com/Leko/godemo/commit/2e34efbeb9ce75e17a902e1f3fd6cf89dcf14599\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">このように</a>変えるだけで良くなりました。</p>\n<h2 id=\"shell-commandモジュールをなるべく使わない\" style=\"position:relative;\"><a href=\"#shell-command%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%82%92%E3%81%AA%E3%82%8B%E3%81%B9%E3%81%8F%E4%BD%BF%E3%82%8F%E3%81%AA%E3%81%84\" aria-label=\"shell commandモジュールをなるべく使わない permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>shell, commandモジュールをなるべく使わない</h2>\n<p>shell, commandモジュールはシェルなので便利ですが、代わりに全ての操作が<code>changed</code>になってしまいます。<br>\nこれでは <strong>プロビジョニングを実行した結果何が変わったのか</strong> が分かりません。安心感に欠けます。</p>\n<p>幸い、それらのモジュールを使わないで済む代替手段が色々あったので、なるべくそちらに寄せていきます。 ファイルの上書きなどで冪等性をシェルだけで担保しようとするとかなり煩雑になってしまうので、とても有り難いです。<br>\n以下に何かと使いそうな例と、それを利用したリファクタの差分を出してみました。</p>\n<ul>\n<li>ファイルの存在チェックなら<a href=\"http://docs.ansible.com/ansible/stat_module.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">stat</a>モジュール (<a href=\"https://github.com/Leko/godemo/commit/2057011dc30720be1c7833a89676cc436a950c26\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">差分</a>)</li>\n<li>apt-update, apt-upgradeは<a href=\"http://docs.ansible.com/ansible/apt_module.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">apt</a>モジュール(<a href=\"https://github.com/Leko/godemo/commit/6d3f26a9c9759e93474cf3a0d1c7d775cb6fae5d\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">差分</a>)</li>\n<li>ファイルの末尾に挿入、ファイル内の編集は<a href=\"http://docs.ansible.com/ansible/lineinfile_module.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">lineinfile</a>モジュール(<a href=\"https://github.com/Leko/godemo/commit/6b1ed78b93a9042d45b211f7361be6988f592dc6\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">差分</a>)</li>\n</ul>\n<h2 id=\"shell-commandモジュールを使うならchanged_whenをつける\" style=\"position:relative;\"><a href=\"#shell-command%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E3%82%92%E4%BD%BF%E3%81%86%E3%81%AA%E3%82%89changed_when%E3%82%92%E3%81%A4%E3%81%91%E3%82%8B\" aria-label=\"shell commandモジュールを使うならchanged_whenをつける permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>shell, commandモジュールを使うならchanged_whenをつける</h2>\n<p>それでもやっぱり、シェルにお力添え頂きたい箇所は出てしまいます。<br>\nそんなときは<code>changed_when</code>というプロパティを足して、実行した結果何か変わったのか否か？ を明示してあげると、シェル系のモジュールを使用してもokが出るようになります。</p>\n<p>例えば<a href=\"https://github.com/Leko/godemo/commit/d4c0b6ff86e06ef6880ca5c350d82ad510feb02c\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Goのバージョンチェック</a>では標準出力の内容に指定した文字列が含まれるか否かでchangedを出しています。</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> check Go version\n    <span class=\"token key atrule\">shell</span><span class=\"token punctuation\">:</span> go version <span class=\"token punctuation\">|</span> awk '<span class=\"token punctuation\">{</span> print $3 <span class=\"token punctuation\">}</span>'\n    <span class=\"token key atrule\">register</span><span class=\"token punctuation\">:</span> go_version\n    <span class=\"token key atrule\">ignore_errors</span><span class=\"token punctuation\">:</span> yes\n    <span class=\"token key atrule\">changed_when</span><span class=\"token punctuation\">:</span> <span class=\"token string\">\"go_version.stdout.find('go{{ goversion }}')\"</span></code></pre></div>\n<p>例えば<a href=\"https://github.com/Leko/godemo/commit/cde5d3d48f24967d83402b23699b04fb09fd87be\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">godep save</a>では、終了コードも標準出力の内容も違わないため無理かと思いましたが、ゴリ推しました。<br>\ngodep save実行後に<code>Godeps/Godeps.json</code>のdiffを見てもし差分が出ていればchangedという判定にしました。</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> save go dependencies\n    <span class=\"token key atrule\">shell</span><span class=\"token punctuation\">:</span> cd <span class=\"token punctuation\">{</span><span class=\"token punctuation\">{</span> apppath <span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span> <span class=\"token important\">&amp;&amp;</span> go get <span class=\"token important\">&amp;&amp;</span> godep save <span class=\"token important\">&amp;&amp;</span> git diff Godeps/Godeps.json\n    <span class=\"token key atrule\">register</span><span class=\"token punctuation\">:</span> godep_diff\n    <span class=\"token key atrule\">changed_when</span><span class=\"token punctuation\">:</span> <span class=\"token string\">\"godep_diff.stdout != ''\"</span></code></pre></div>\n<h2 id=\"結果\" style=\"position:relative;\"><a href=\"#%E7%B5%90%E6%9E%9C\" aria-label=\"結果 permalink\" class=\"autolink-header before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>結果</h2>\n<p>実行結果のgifアニメを撮ってみました。</p>\n<p><img src=\"/aecb502d6f74c1d4141b7b9786d74859/Pw4PrxxEfo1.gif\" alt=\"Pw4PrxxEfo\"></p>\n<p>二回目の実行では全部okになります。<br>\nこれで(シェルがバグってなければ)冪等性が担保できるようになりました。</p>\n<p><del>今回の場合は開発環境の初期構築しかやってないので最初の一回しか実行する必要無いんですが、</del>冪等性のある設定がきちんと書けるようになっていると必要なときに何かと役立つと思います。<br>\nまだまだAnsibleの知識と慣れが不足しているので、よりよい書き方などご教授頂けると幸いです。</p>","timeToRead":5,"frontmatter":{"title":"Goの環境を作るAnsibleの設定をリファクタした","tags":["Ansible","Go"],"date":"December 12, 2015","featuredImage":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='125'%20viewBox='0%200%20400%20125'%20preserveAspectRatio='none'%3e%3cpath%20d='M0%2063v62h401V0H0v63m0%200'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":3.2115384615384617,"src":"/static/d9a1348cd0ccec1cb251c17b74ba6792/0eba5/featured-image.png","srcSet":"/static/d9a1348cd0ccec1cb251c17b74ba6792/1ec58/featured-image.png 334w,\n/static/d9a1348cd0ccec1cb251c17b74ba6792/ccb4a/featured-image.png 668w,\n/static/d9a1348cd0ccec1cb251c17b74ba6792/0eba5/featured-image.png 1284w","srcWebp":"/static/d9a1348cd0ccec1cb251c17b74ba6792/1a357/featured-image.webp","srcSetWebp":"/static/d9a1348cd0ccec1cb251c17b74ba6792/cd98f/featured-image.webp 334w,\n/static/d9a1348cd0ccec1cb251c17b74ba6792/7535d/featured-image.webp 668w,\n/static/d9a1348cd0ccec1cb251c17b74ba6792/1a357/featured-image.webp 1284w","sizes":"(max-width: 1284px) 100vw, 1284px"}}}}}},"pageContext":{"slug":"/refactored-ansible-settings-to-setup-golang/","previous":{"fields":{"slug":"/how-to-validate-form-value-with-constraint-validation/"},"frontmatter":{"title":"HTML5のConstraint validationでライブラリ要らずならくらくバリデーション","tags":["Advent Calendar","HTML5 Constraint validation","JavaScript"]}},"next":{"fields":{"slug":"/how-to-disable-limitation-of-cors-in-html5-canvas/"},"frontmatter":{"title":"canvasのCORS制限を突破する","tags":["HTML5 Canvas","JavaScript","security"]}}}},
    "staticQueryHashes": ["2585454260","2954598359"]}