You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Eric Chang">
<meta name="description" content="Bilberry Premium Theme for Hugo.">
<meta name="keywords" content="blog,personal,responsive,search,font awesome,pages,posts,multilingual,highlight.js,syntax highlighting,premium,shortcuts">
<meta name="generator" content="Hugo 0.50" />
<title> [筆記] Ansible how to use &#39;list&#39; in yaml file | MC部落</title>
<meta name="description" content="[筆記] Ansible how to use &#39;list&#39; in yaml file - Bilberry Premium Theme for Hugo.">
<meta itemprop="name" content="[筆記] Ansible how to use &#39;list&#39; in yaml file ">
<meta itemprop="description" content="[筆記] Ansible how to use &#39;list&#39; in yaml file - Bilberry Premium Theme for Hugo.">
<meta property="og:title" content="[筆記] Ansible how to use &#39;list&#39; in yaml file ">
<meta property="og:description" content="[筆記] Ansible how to use &#39;list&#39; in yaml file - Bilberry Premium Theme for Hugo.">
<meta property="og:image" content="https://h.cowbay.org/images/post-default-1.jpg">
<meta property="og:url" content="https://h.cowbay.org/post/ansible-selectattr/">
<meta property="og:site_name" content="MC部落">
<meta property="og:type" content="article">
<link rel="icon" type="image/png" href="https://h.cowbay.org/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="https://h.cowbay.org/favicon-16x16.png" sizes="16x16">
<link rel="stylesheet" href="https://h.cowbay.org/sass/combined.min.717098cb5503581e75f12e486a847ca410bf8367d4d8713f4c37affc868c5a1d.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body class="bilberry-hugo-theme">
<nav class="permanentTopNav">
<div class="container">
<ul class="topnav">
</ul>
<div id="search-box" class="search">
<i class="fa fa-search"></i>
<input id="search" type="text" placeholder="Search ...">
</div>
</div>
</nav>
<header>
<div class="container">
<div class="logo">
<a href="/" class="logo">
<img src="https://www.gravatar.com/avatar/e4eb1f8e016ffb73e9889f87d16e15f0?d=mm&size=200" alt="">
<span class="overlay"><i class="fa fa-home"></i></span>
</a>
</div>
<div class="titles">
<h3 class="title"><a href="/">MC部落</a></h3>
<span class="subtitle">這是MC的HUGO部落格採用bilberry theme</span>
</div>
<div class="toggler permanentTopNav">
<i class="fa fa-bars" aria-hidden="true"></i>
</div>
</div>
</header>
<div class="main container">
<div class="article-wrapper u-cf single">
<a class="bubble" href="/post/ansible-selectattr/">
<i class="fa fa-fw fa-pencil"></i>
</a>
<article class="default article">
<div class="featured-image">
<a href="/post/ansible-selectattr/">
<img src="/images/post-default-1.jpg" alt="">
</a>
</div>
<div class="content">
<h3><a href="/post/ansible-selectattr/">[筆記] Ansible how to use &#39;list&#39; in yaml file </a></h3>
<div class="meta">
<span class="date moment">2018-11-27</span>
<span class="categories">
<a href="/categories/%E7%AD%86%E8%A8%98">筆記</a>
</span>
<span class="author"><a href="/author/eric-chang">Eric Chang</a></span>
</div>
<p>這幾天在玩ansible 時,碰到一個問題</p>
<p>假如我有個yaml檔作為資料來源檔名是 abc.yml</p>
<p>大概長這樣</p>
<pre><code> &quot;teams&quot;: [
{
&quot;chinese_name&quot;: &quot;TEAM1&quot;,
&quot;description&quot;: &quot;TEAM1&quot;,
&quot;gid&quot;: 10125,
&quot;location&quot;: [
&quot;hq&quot;
],
&quot;name&quot;: &quot;aa&quot;,
&quot;users&quot;: [
&quot;chen&quot;,
&quot;chou&quot;,
&quot;huani&quot;,
&quot;yey&quot;,
&quot;wa&quot;
]
},
{
&quot;chinese_name&quot;: &quot;TEAM2&quot;,
&quot;description&quot;: &quot;TEAM2&quot;,
&quot;gid&quot;: 10126,
&quot;location&quot;: [
&quot;hq&quot;
],
&quot;name&quot;: &quot;bb&quot;,
&quot;users&quot;: [
&quot;chhiao&quot;,
&quot;chgc&quot;,
&quot;chy&quot;,
&quot;hsi&quot;,
&quot;li&quot;,
&quot;li&quot;,
&quot;chgchi&quot;
]
}
]
</code></pre>
<p></p>
<p>稍微整理一下,比較容易看</p>
<pre><code> &quot;teams&quot;: [
{
&quot;chinese_name&quot;: &quot;TEAM1&quot;,
&quot;description&quot;: &quot;TEAM1&quot;,
&quot;gid&quot;: 10125,
&quot;location&quot;: [&quot;hq&quot;],
&quot;name&quot;: &quot;aa&quot;,
&quot;users&quot;: [&quot;chen&quot;,&quot;chou&quot;,&quot;huani&quot;,&quot;yey&quot;,&quot;wa&quot;]
},
{
&quot;chinese_name&quot;: &quot;TEAM2&quot;,
&quot;description&quot;: &quot;TEAM2&quot;,
&quot;gid&quot;: 10126,
&quot;location&quot;: [&quot;hq&quot;],
&quot;name&quot;: &quot;bb&quot;,
&quot;users&quot;: [&quot;chhiao&quot;,&quot;chgc&quot;,&quot;chy&quot;,&quot;hsi&quot;,&quot;li&quot;,&quot;chgchi&quot;]
}
]
</code></pre>
<p>在 ansible playbook 中,我用 include_vars 把這個檔案叫進來</p>
<pre><code>- name: load teams.yml
tags: env
include_vars:
file: files/konwen/teams.yml
</code></pre>
<p>這時候在這個執行階段,就會有一個變數叫 teams 裡面有 chinese_name/description/gid 等等這些屬性</p>
<p>其中的 location/users 又是另外一個 list</p>
<p>那如果我想要用users這個清單中的id作為建立帳號的來源</p>
<p>我就可以用底下這段,先把 users 裡面的內容,指給 dc_users 這個 localvar</p>
<p>然後加上 when 的條件,限定只有 name == aa 的 users 才會被指定給 dc_users</p>
<pre><code>- name: set aa_users
tags: env
set_fact:
aa_users: &quot;{{ item.users }}&quot;
when: item.name == 'aa'
with_items: &quot;{{ teams }}&quot;
</code></pre>
<p>這樣子執行下來是沒有問題的,不過就是醜了點 XD</p>
<p>之後要抓 user 帳號時,就可以直接用 aa_users 來跑迴圈</p>
<pre><code>- name: create folder for aa_users
tags: env
file:
path: &quot;/tmp/{{ item }}&quot;
owner: &quot;{{ item }}&quot;
group: &quot;{{ item }}&quot;
state: directory
with_items: &quot;{{ aa_users }}&quot;
</code></pre>
<p>很簡單的概念,因為一開始在 teams 這個 var 裡面</p>
<p>users 這個屬性是一個 list</p>
<p>p.s 講話一定要參雜用英文單字,這樣看起來比較屌&hellip;</p>
<p>所以沒辦法直接在底下的create folder task 直接叫出來</p>
<p>如果直接叫 teams.users ,那會是一個清單</p>
<pre><code> [&quot;chen&quot;,&quot;chou&quot;,&quot;huani&quot;,&quot;yey&quot;,&quot;wa&quot;]
</code></pre>
<p>然後 ansible 也很厲害,就這個樣子,他還是會去忠實的執行建立目錄</p>
<p>所以在 /tmp 底下就會多出一個</p>
<blockquote>
<p>[&ldquo;chen&rdquo;,&ldquo;chou&rdquo;,&ldquo;huani&rdquo;,&ldquo;yey&rdquo;,&ldquo;wa&rdquo;]</p>
</blockquote>
<p>這個樣子的目錄 &hellip;.</p>
<p>所以我的處理方式就是先把這個清單丟給一個local變數 ( aa_users )</p>
<p>後面的task再用這個 {{ aa_users }} 來跑,這樣子就 OK 了</p>
<p>雖說很簡單,但是卡了我一整天吶&hellip;</p>
</div>
<div class="footer">
<div class="tags">
<i class="fa fa-tags"></i>
<div class="links">
<a href="/tags/ansible">ansible</a>
<a href="/tags/linux">linux</a>
</div>
</div>
</div>
</article>
</div>
<div id="disqus_thread"></div>
<script type="application/javascript">
var disqus_config = function () {
};
(function() {
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
return;
}
var d = document, s = d.createElement('script'); s.async = true;
s.src = '//' + "h-cowbay-org-1" + '.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
<footer>
<div class="container">
<div class="recent-posts">
<strong>Latest posts</strong>
<ul>
<li>
<a href="/post/create-portable-vim-environment/">[筆記] 建立一個帶著走的 VIM 環境 Creating portable Vim environment</a>
</li>
<li>
<a href="/post/synology-ds415-repair-cost/">[雜念] 群暉 Synology NAS DS 415&#43; 誇張的維修費用</a>
</li>
<li>
<a href="/post/10g-lab-using-proxmox-and-mellanox/">[筆記] 用 proxmox &amp; Mellanox SFP 網卡土炮 10G LAB </a>
</li>
<li>
<a href="/post/ansible-selectattr-filter/">[筆記] 還是 Ansible Selectattr </a>
</li>
<li>
<a href="/post/ansible-selectattr/">[筆記] Ansible how to use &#39;list&#39; in yaml file </a>
</li>
<li>
<a href="/post/change-preferred-language-in-firefox/">[筆記] 為了metabase 修改 firefox 開啟網頁時使用的預設語言 change the preferred language in firefox for metabase</a>
</li>
<li>
<a href="/post/copy_role_in_pgsql/">PostgreSQL 直接從已經存在的使用者複製權限到另一個使用者</a>
</li>
</ul>
</div>
<div class="categories">
<a href="/categories/"><strong>Categories</strong></a>
<ul>
<li>
<a href="/categories/%E7%AD%86%E8%A8%98">筆記 (10)</a>
</li>
<li>
<a href="/categories/ps">Ps (1)</a>
</li>
<li>
<a href="/categories/%E7%BE%A4%E6%9A%89">群暉 (1)</a>
</li>
</ul>
</div>
<div class="right">
<div class="external-profiles">
<strong>Social media</strong>
<a href="https://www.facebook.com/mariahchang" target="_blank"><i class="fa fa-facebook-adblock-proof"></i></a>
<a href="https://twitter.com/changchichung" target="_blank"><i class="fa fa-twitter-adblock-proof"></i></a>
<a href="https://github.com/changchichung" target="_blank"><i class="fa fa-github"></i></a>
</div>
</div>
</div>
</footer>
<div class="credits">
<div class="container">
<div class="copyright">
<a href="https://github.com/Lednerb" target="_blank">
&copy;
2017
by Lednerb
</a>
</div>
<div class="author">
<a href="https://github.com/Lednerb/bilberry-hugo-theme" target="_blank">Bilberry Hugo Theme</a>
</div>
</div>
</div>
<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-128770427-1', 'auto');
ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<script type="text/javascript" src="https://h.cowbay.org/js/externalDependencies.39c47e10e241eae2947b3fe21809c572.js" integrity="md5-OcR&#43;EOJB6uKUez/iGAnFcg=="></script>
<script type="text/javascript" src="https://h.cowbay.org/js/theme.ff50ae6dc1bfc220b23bf69dbb41b54e.js" integrity="md5-/1CubcG/wiCyO/adu0G1Tg=="></script>
<script>
$(".moment").each(function() {
$(this).text(
moment( $(this).text() )
.locale( "en" )
.format('LL')
);
});
$(".footnote-return sup").html("");
</script>
<script>
var client = algoliasearch("2XL0P8XDCY", "4ef65b37b627bb886b46c34a10e63aa6");
var index = client.initIndex("h_cowbay_org");
$('#search').autocomplete({ hint: false, autoselect: true, debug: false },
[
{
source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 10 }),
displayKey: function(suggestion) {
return suggestion.title || suggestion.author
},
templates: {
suggestion: function(suggestion) {
return "<span class='entry " + suggestion.type + "'>"
+ "<span class='title'>" + suggestion.title + "</span>"
+ "<span class='fa fa-fw " + suggestion.iconClass + "'></span>"
+ "</span>"
;
},
empty: function() {
return "<span class='empty'>Nothing found.</span>"
},
footer: function() {
return '<div class="branding">Powered by <img src="https:\/\/h.cowbay.org\/dist\/algolia-logo-light.svg" /></div>'
}
},
}
])
.on('autocomplete:selected', function(event, suggestion, dataset) {
window.location = (suggestion.url);
})
.keypress(function (event, suggestion) {
if (event.which == 13) {
window.location = (suggestion.url);
}
});
</script>
</body>
</html>