自動化運維利器(Ansible)三

2021-10-22 16:59:34 字數 2307 閱讀 6258

它本身是乙個文字檔案,乙個格式類似的ini檔案。

自定義資產

這個檔案可以自定義,之後使用 -i 引數指定

下面給出乙個自定義的靜態資產例項,然後再具體介紹其含義。

# cat inventory.ini

1.1.1.1

2.2.2.2

3.3.3.[1:15]

test01.qfedu.com

test03.qfedu.com

test[05:09].qfedu.com

[web_servers]

192.168.1.2

192.168.1.3

192.168.1.5

[db_servers]

192.168.2.2

192.168.2.3

192.168.1.5

[all_servers]

[all_servers:children]

#這裡是把前面的組以子組的方式加入到新的組裡面

db_servers

web_servers

如何使用自定義資產

通過 -i 引數指定自定義資產的位置即可(可以是絕對路徑,也可以是相對路徑)。

# ansible all -i inventory ... // 偽指令,不可執行
如何驗證自定義資產

假如我們剛剛定義的資產為inventory.ini

# ansible all -i inventory  --list-hosts

hosts (29):

1.1.1.1

2.2.2.2

3.3.3.1

...略...

# ansible web_servers -i inventory --list-hosts

hosts (3):

192.168.2.2

192.168.2.3

192.168.1.5

資產選擇器
ansible  pattern -i inventory -m module -a argument

ansible 選擇主機列表裡面的主機/組 -i 主機列表 -m 模組 -a 引數

# ansible 1.1.1.1 -i inventory --list-hosts

hosts (1):

1.1.1.1

# ansible test01.qfedu.com -i inventory --list-hosts

hosts (1):

test01.qfedu.com

# ansible 1.1.1.1,2.2.2.2 -i inventory --list-hosts

hosts (2):

1.1.1.1

2.2.2.2

# ansible web_servers -i inventory --list-hosts

hosts (3):

192.168.1.2

192.168.1.3

192.168.1.5

# ansible 3.3.3.1* -i inventory --list-hosts

hosts (7):

3.3.3.13

3.3.3.10

3.3.3.11

3.3.3.12

3.3.3.14

3.3.3.15

3.3.3.1

使用邏輯匹配

# ansible 'web_servers:db_servers' -i inventory --list-hosts

hosts (5):

192.168.1.2

192.168.1.3

192.168.1.5

192.168.2.2

192.168.2.3

# ansible 'web_servers:&db_servers' -i inventory --list-hosts

hosts (1):

192.168.1.5

# ansible 'web_servers:!db_servers' -i inventory --list-hosts

hosts (2):

192.168.1.2

192.168.1.3

自動化運維利器(Ansible)二

root lwq.com ssh keygen t rsa f ssh id rsa n 非互動式建立金鑰將本地的公鑰傳輸到被管理節點 root lwq.com ssh copy id root 192.66.66.102 root lwq.com ssh copy id root 192.66.6...

自動化運維ansible

sever1 172.25.60.1 server2 172.25.60.2 server3 172.25.60.3 etc ansible ansible.cfg 主配置檔案,配置ansible工作特性 etc ansible hosts 主機清單 etc ansible roles 存放角色的目...

ansible 自動化運維工具

ansible 自動化運維工具 批量管理 python開發 無客戶端 基於ssh服務 22 安裝 1.ansible包在擴充套件源,epel release yum y install epel release yum repolist 2.安裝,ansible yum y install ansi...