ansible中的hostvars


首先來看一個例子:假設我想得到主機IP為172.25.250.9的完全限定域名(FQDN),但是我無法登錄該主機,那么就可以用本機里面的hostvars魔法變量(后面會分享我對魔法這個詞的理解)這個字典里面的ansible_fact這個子字典(后面會補充解釋)來得到。

首先讓我們來看看hostvars里面都是些啥,我們可以通過一下命令來查看hostvars這個字典:

ansible locahost -m debug -a “var=hostvars"
localhost | SUCCESS => {
    "hostvars": {
        "172.25.250.10": {
            "ansible_check_mode": false,
            "ansible_diff_mode": false,
            "ansible_facts": {},
            "ansible_forks": 5,
            "ansible_inventory_sources": [
                "/home/greg/ansible/inventory"
            ],
            "ansible_password": "redhat",
            "ansible_playbook_python": "/usr/libexec/platform-python",
            "ansible_user": "root",
            "ansible_verbosity": 0,
            "ansible_version": {
                "full": "2.8.0",
                "major": 2,
                "minor": 8,
                "revision": 0,
                "string": "2.8.0"
            },
            "group_names": [
                "test"
            ],
            "groups": {
                "all": [
                    "172.25.250.9",
                    "172.25.250.10",
                    "172.25.250.13",
                    "172.25.250.11",
                    "172.25.250.12"
                ],
                "balancers": [
                    "172.25.250.13"
                ],
                "dev": [
                    "172.25.250.9"
                ],
                "prod": [
                    "172.25.250.11",
                    "172.25.250.12"
                ],
                "test": [
                    "172.25.250.10"
                ],
                "ungrouped": [],
                "webservers": [
                    "172.25.250.11",
                    "172.25.250.12"
                ]
            },
            "inventory_dir": "/home/greg/ansible",
            "inventory_file": "/home/greg/ansible/inventory",
            "inventory_hostname": "172.25.250.10",
            "inventory_hostname_short": "172",
            "omit": "__omit_place_holder__24431a21232ab0a628ff5c537f472e6b49d1e14a",
            "playbook_dir": "/home/greg/ansible"
        }, "172.25.250.11": {
            "ansible_check_mode": false,
            "ansible_diff_mode": false,
            "ansible_facts": {},
             ....................
        "172.25.250.12": {
            "ansible_check_mode": false,
            "ansible_diff_mode": false,
            "ansible_facts": {},
            "ansible_forks": 5,
            "ansible_inventory_sources": [
                "/home/greg/ansible/inventory"
            ],
            ..........................
        "172.25.250.13": {
            "ansible_check_mode": false,
            "ansible_diff_mode": false,
            "ansible_facts": {},
            "ansible_forks": 5,
            "ansible_inventory_sources": [
                "/home/greg/ansible/inventory"
            ],
            ............................
        "172.25.250.9": {
            "ansible_check_mode": false,
            "ansible_diff_mode": false,
            "ansible_facts": {},
            "ansible_forks": 5,
            "ansible_inventory_sources": [
                "/home/greg/ansible/inventory"
            ],
     ......................
            "inventory_dir": "/home/greg/ansible",
            "inventory_file": "/home/greg/ansible/inventory",
            "inventory_hostname": "172.25.250.9",
            "inventory_hostname_short": "172",
            "omit": "__omit_place_holder__24431a21232ab0a628ff5c537f472e6b49d1e14a",
            "playbook_dir": "/home/greg/ansible"
        }
    }
}

從輸出結果可以看到,在hostvars里面一共有5個鍵值對(‘172.25.250.9’:值1,‘172.25.250.13:值2這樣的形式。因為我的環境里一共有5個受控節點,所以有五個鍵值對),每個鍵值對的值里面都有一個”ansible_facts“的鍵,這個鍵對應的值便是生成f的acts(有關遠程主機節點的信息)。

Tip: hostvars里面還有groups這個魔法變量(可以從最上面的輸出看到),里面包含了5個節點的IP,也就是all這個組,(所有節點都會被自動划分到all組里面,相對應的,如果一個組沒有加入了除all組之外的組,那么這個主機還會被自動划分到ungrouped組里面,也就是說,ungrouped組里面的主機都是沒有加入別的組的(除了all組))

 

好了,現在可以說:fqdn可以通過ansible_facts里面的某個元素來得到,既然我們知道hostvars里面有ansible_facts,那么現在讓我們再來看看ansible_facts里面都有些啥。

ansible localhost -m setup -a "filter=*fqdn*"
# localhost: 執行節點為本機
# -m: module
# setup: 生成ansible_facts
# -a: append
# filter: 前面的參數組成的命令會一股腦的全部輸出,filter參數用於過濾這些輸出
# *fqdn*: 正則表達式,含有fqdn的項

 

 

 Tip:可以直接執行“ansible localhost -m setup”來查看所有的ansible_facts,由於太冗余這里不再顯示。

從上圖我們可以看到,ansible_facts里面的ansible_fqdn存儲着localhost(左上角顯示的是localhost)的fqdn。

所以我們可以通過hostvars---->ansible_facts------->ansible_fqdn來得到fqdn,但是hostvars里面有很多不同主機的ansible_facts,所以我們要指定是哪台主機的ansible_facts,這樣才能得到正確的fqdn,所以可以通過以下方式得到:

hostvars["172.25.250.9"]["ansible_facts"]["ansible_fqdn"]

 
        

對“魔法”的理解:

If you want to configure your database server using the value of a ‘fact’ from another node, or the value of an inventory variable assigned to another node, you can use hostvars in a template or on an action line

如果我們想用來自別的節點的信息,那么就可以在jinja或命令行中用hostvars這個魔法變量,我個人的理解是:其“魔法”體現在明明是本機,卻可以得到別的主機的信息,所以就很神奇。

With hostvars, you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using the hostvars variable too, but only after you have gathered (or cached) facts.

我們還可以通過hostvars變量獲取ansible_facts。

文檔鏈接:

Discovering variables: facts and magic variables — Ansible Documentation

在文檔內搜索:“Information about Ansible”即可定位到。

 

在這里問廣大網友一個問題,本人剛接觸ansible,不理解jinja模板中調用這些魔法變量時為什么可以不先定義的,就是說我想知道ansible是怎么調用jinja文件的,或者是jinja是怎么獲取那些沒有實現定義在jinja文件中的魔法變量(例如groups)的?懇請各位傳道授業,本人不勝感激。

更新:

  今天在看文章的時候突然看到了自己想要的答案,雖然這個答案很簡單,但是就是這個簡單的答案能夠讓我身心稍微有點舒暢。

 

  也就是說,這些魔法變量是ansible自動提供的,我們不需要定義,play-book在運行時,ansible會自動匹配魔法變量,然后賦值。

  原文鏈接:Ansible inventory_hostname | Examples of Ansible inventory_hostname (educba.com),這篇文章稍微看一下,可以理解inventory_hostname的值是從哪里來的。(就是你定義的主機清單里面的主機名,所以如果你把主機名直接定義為主機IP,那么inventory_name就可以是你的主機的IP)

  另外,ansible文檔中說這些魔法變量是保留的,我們不能使用。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM