diff --git a/content/post/ansible-selectattr-from-list-in-dictionary.md b/content/post/ansible-selectattr-from-list-in-dictionary.md new file mode 100644 index 00000000..d232b85b --- /dev/null +++ b/content/post/ansible-selectattr-from-list-in-dictionary.md @@ -0,0 +1,100 @@ +--- +title: "[ansible] 引用事先定義好的yaml檔裡面的變數 - Ansible Selectattr From List in Dictionary file" +date: 2019-07-01T09:06:12+08:00 +draft: false + +noSummary: false +featuredImage: "https://h.cowbay.org/images/post-default-7.jpg" +categories: ['Ansible'] +tags: ['Ansible'] +author: "Eric Chang" +--- + +在ansible中,關於如何引用自定義的變數,一直讓我很頭疼 + +尤其是有牽涉到從外部導入yaml檔案時,更是常常讓我不知道到底該怎麼抓出想要的變數 + +這次還是用selectattr 來處理,希望下次能夠記得... + + + +首先是導入的yaml檔案,內容長這樣 + + +``` +client_hosts: + abc.com: + - host: hqdc021 + ipv4: 192.168.11.21 + - host: hqdc022 + ipv4: 192.168.11.22 + - host: hqdc023 + ipv4: 192.168.11.23 + - host: hqdc024 + ipv4: 192.168.11.24 + - host: hqdc025 + iuser: True + ipv4: 192.168.11.25 + user: [yangj] + - host: hqdc026 + ipv4: 192.168.11.26 + user: [changp, joy] + - host: hqdc027 + ipv4: 192.168.11.27 + xyz.com: + - host: dc021 + iuser: True + ipv4: 192.168.1.21 + user: [tim] + - host: dc022 + ipv4: 192.168.1.22 + - host: dc023 + ipv4: 192.168.1.23 + - host: dc024 + ipv4: 192.168.1.24 + - host: dc025 + ipv4: 192.168.1.25 + user: [eric] + - host: dc026 + ipv4: 192.168.1.26 + user: [erica] + - host: dc027 + ipv4: 192.168.1.27 + + + +``` +在playbook中,首先先導入這個檔案 +``` +- name: load client_host + include_vars: + file: client_hosts.yml + name: ch +``` + +然後用這個剛剛導入的檔案,去做出想要的清單 + +像底下這個,就是指定了client_hosts的 abc.com 這個域名底下,iuser有被定義的資料,再轉成list + +``` +- name: get internet user list + set_fact: + iuser_list: "{{ ch['client_host']['abc.com']|selectattr('iuser','defined')|list }}" +``` + +然後就可以用來做condition了 + +``` +- name: copy environment block to /etc/environment + copy: + content: | + PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" + all_proxy="{{ proxy_env }}" + http_proxy="{{ proxy_env }}" + https_proxy="{{ proxy_env }}" + no_proxy="localhost,127.0.0.1,192.168.1.1/16,.xyz.com,.abc.com" + dest: /etc/environment + when: item.ipv4 == ansible_default_ipv4.address + with_items: "{{ iuser_list }}" +``` + diff --git a/public/404.html b/public/404.html index e82254a4..f0b08d5f 100644 --- a/public/404.html +++ b/public/404.html @@ -115,6 +115,10 @@ @@ -156,6 +156,10 @@ 筆記 (20) +
  • + Ansible (1) +
  • +
  • Linux (1)
  • diff --git a/public/author/eric-chang/index.html b/public/author/eric-chang/index.html index 3baa2067..45f7a9ab 100644 --- a/public/author/eric-chang/index.html +++ b/public/author/eric-chang/index.html @@ -90,6 +90,83 @@ +
    + + + + + +
    + + + + +
    +

    [ansible] 引用事先定義好的yaml檔裡面的變數 - Ansible Selectattr From List in Dictionary file

    +
    + + + 2019-07-01 + + + + + + + + + Ansible + + + + + + Eric Chang + +
    + + +

    在ansible中,關於如何引用自定義的變數,一直讓我很頭疼

    + +

    尤其是有牽涉到從外部導入yaml檔案時,更是常常讓我不知道到底該怎麼抓出想要的變數

    + +

    這次還是用selectattr 來處理,希望下次能夠記得…

    + +

    + + + + + +
    + + + + +
    + + +
    +
    @@ -780,87 +857,6 @@ -
    - - - - - - -
    - - - - - -
    - - - - -
    -

    Install Timeshift on Ubuntu1804

    -
    - - - 2019-03-11 - - - - - - - - - 筆記 - - - - - - Eric Chang - -
    - - -

    最近要開始測試client安裝 ubuntu 18.04 的 ansible playbook

    - -

    因為要不斷的修正,所以想到一直有在自己電腦上執行的timeshift這個軟體

    - -

    可以很簡單快速的備份、恢復系統狀態

    - -

    可是不知道為什麼,在ubuntu 18.04 上安裝就是會發生錯誤….

    - -

    - - - - - -
    - - -
    @@ -891,6 +887,10 @@
    @@ -932,6 +928,10 @@ 筆記 (20) +
  • + Ansible (1) +
  • +
  • Linux (1)
  • diff --git a/public/author/eric-chang/index.xml b/public/author/eric-chang/index.xml index 252eb4de..d6aca8d1 100644 --- a/public/author/eric-chang/index.xml +++ b/public/author/eric-chang/index.xml @@ -5,11 +5,26 @@ https://h.cowbay.org/author/eric-chang/ Recent content in Eric Chang on MCの飄狂山莊㊣ Hugo -- gohugo.io - Thu, 20 Jun 2019 11:03:12 +0800 + Mon, 01 Jul 2019 09:06:12 +0800 + + [ansible] 引用事先定義好的yaml檔裡面的變數 - Ansible Selectattr From List in Dictionary file + https://h.cowbay.org/post/ansible-selectattr-from-list-in-dictionary/ + Mon, 01 Jul 2019 09:06:12 +0800 + + https://h.cowbay.org/post/ansible-selectattr-from-list-in-dictionary/ + <p>在ansible中,關於如何引用自定義的變數,一直讓我很頭疼</p> + +<p>尤其是有牽涉到從外部導入yaml檔案時,更是常常讓我不知道到底該怎麼抓出想要的變數</p> + +<p>這次還是用selectattr 來處理,希望下次能夠記得&hellip;</p> + +<p></p> + + linux底下遠端遙控&管理的好用系統 Meshcentral / Remote Management & control system Meshcentral https://h.cowbay.org/post/remote-management-system-meshcentral/ diff --git a/public/author/eric-chang/page/2/index.html b/public/author/eric-chang/page/2/index.html index f769bec4..a433141b 100644 --- a/public/author/eric-chang/page/2/index.html +++ b/public/author/eric-chang/page/2/index.html @@ -90,6 +90,87 @@ +
    + + + + + +
    + + + + +
    +

    Install Timeshift on Ubuntu1804

    +
    + + + 2019-03-11 + + + + + + + + + 筆記 + + + + + + Eric Chang + +
    + + +

    最近要開始測試client安裝 ubuntu 18.04 的 ansible playbook

    + +

    因為要不斷的修正,所以想到一直有在自己電腦上執行的timeshift這個軟體

    + +

    可以很簡單快速的備份、恢復系統狀態

    + +

    可是不知道為什麼,在ubuntu 18.04 上安裝就是會發生錯誤….

    + +

    + + + + + +
    + + + + +
    + + +
    +
    @@ -854,89 +935,6 @@ -
    - - - - - - -
    - - - - - -
    - - - - -
    -

    [筆記] 為了metabase 修改 firefox 開啟網頁時使用的預設語言 change the preferred language in firefox for metabase

    -
    - - - 2018-11-15 - - - - - - - - - 筆記 - - - - - - Eric Chang - -
    - - -

    最近在測試metabase,記得幾個月前就有測試過

    - -

    但是當時的界面和現在的樣子差很多,看樣子改版還滿勤勞的

    - -

    所以這次改用docker來建立,根本五分鐘不到就建好了(挖鼻孔)

    - -

    不過呢,很討厭的是,一進去就發現語系採用的是簡體中文

    - -

    - - - - - -
    - - -
    @@ -969,6 +967,10 @@
    @@ -1010,6 +1008,10 @@ 筆記 (20) +
  • + Ansible (1) +
  • +
  • Linux (1)
  • diff --git a/public/author/eric-chang/page/3/index.html b/public/author/eric-chang/page/3/index.html index f7b12f84..7d066744 100644 --- a/public/author/eric-chang/page/3/index.html +++ b/public/author/eric-chang/page/3/index.html @@ -90,6 +90,89 @@ +
    + + + + + +
    + + + + +
    +

    [筆記] 為了metabase 修改 firefox 開啟網頁時使用的預設語言 change the preferred language in firefox for metabase

    +
    + + + 2018-11-15 + + + + + + + + + 筆記 + + + + + + Eric Chang + +
    + + +

    最近在測試metabase,記得幾個月前就有測試過

    + +

    但是當時的界面和現在的樣子差很多,看樣子改版還滿勤勞的

    + +

    所以這次改用docker來建立,根本五分鐘不到就建好了(挖鼻孔)

    + +

    不過呢,很討厭的是,一進去就發現語系採用的是簡體中文

    + +

    + + + + + +
    + + + + +
    + + +
    +
    @@ -610,6 +693,10 @@
    @@ -651,6 +734,10 @@ 筆記 (20) +
  • + Ansible (1) +
  • +
  • Linux (1)
  • diff --git a/public/author/index.html b/public/author/index.html index c7aa8ddc..ccb031c4 100644 --- a/public/author/index.html +++ b/public/author/index.html @@ -101,7 +101,7 @@
    @@ -120,6 +120,10 @@ @@ -161,6 +161,10 @@ 筆記 (20) +
  • + Ansible (1) +
  • +
  • Linux (1)
  • diff --git a/public/author/index.xml b/public/author/index.xml index 6dc36628..7f55ed52 100644 --- a/public/author/index.xml +++ b/public/author/index.xml @@ -12,7 +12,7 @@ Eric Chang https://h.cowbay.org/author/eric-chang/ - Thu, 20 Jun 2019 11:03:12 +0800 + Mon, 01 Jul 2019 09:06:12 +0800 https://h.cowbay.org/author/eric-chang/ diff --git a/public/categories/ansible/index.html b/public/categories/ansible/index.html new file mode 100644 index 00000000..91a6bd5a --- /dev/null +++ b/public/categories/ansible/index.html @@ -0,0 +1,398 @@ + + + + + + + + + + Ansible | MCの飄狂山莊㊣ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    + +
    +

    MCの飄狂山莊㊣

    + + What’s the Worst That Could Happen? + +
    + + + + +
    + + +
    +
    +
    + + +
    + + + +
    + + + + + +
    + + + + +
    +

    [ansible] 引用事先定義好的yaml檔裡面的變數 - Ansible Selectattr From List in Dictionary file

    +
    + + + 2019-07-01 + + + + + + + + + Ansible + + + + + + Eric Chang + +
    + + +

    在ansible中,關於如何引用自定義的變數,一直讓我很頭疼

    + +

    尤其是有牽涉到從外部導入yaml檔案時,更是常常讓我不知道到底該怎麼抓出想要的變數

    + +

    這次還是用selectattr 來處理,希望下次能夠記得…

    + +

    + + + + + +
    + + + + +
    + + +
    + + + +
    + + + +
    + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/public/categories/ansible/index.xml b/public/categories/ansible/index.xml new file mode 100644 index 00000000..6f5521c6 --- /dev/null +++ b/public/categories/ansible/index.xml @@ -0,0 +1,29 @@ + + + + Ansible on MCの飄狂山莊㊣ + https://h.cowbay.org/categories/ansible/ + Recent content in Ansible on MCの飄狂山莊㊣ + Hugo -- gohugo.io + Mon, 01 Jul 2019 09:06:12 +0800 + + + + + + [ansible] 引用事先定義好的yaml檔裡面的變數 - Ansible Selectattr From List in Dictionary file + https://h.cowbay.org/post/ansible-selectattr-from-list-in-dictionary/ + Mon, 01 Jul 2019 09:06:12 +0800 + + https://h.cowbay.org/post/ansible-selectattr-from-list-in-dictionary/ + <p>在ansible中,關於如何引用自定義的變數,一直讓我很頭疼</p> + +<p>尤其是有牽涉到從外部導入yaml檔案時,更是常常讓我不知道到底該怎麼抓出想要的變數</p> + +<p>這次還是用selectattr 來處理,希望下次能夠記得&hellip;</p> + +<p></p> + + + + \ No newline at end of file diff --git a/public/categories/ansible/page/1/index.html b/public/categories/ansible/page/1/index.html new file mode 100644 index 00000000..fde3c6b8 --- /dev/null +++ b/public/categories/ansible/page/1/index.html @@ -0,0 +1 @@ +https://h.cowbay.org/categories/ansible/ \ No newline at end of file diff --git a/public/categories/index.html b/public/categories/index.html index aef40bc8..a9f06ffa 100644 --- a/public/categories/index.html +++ b/public/categories/index.html @@ -101,6 +101,8 @@