主機定義與分組: 給所有主機部署 key 在 inventory 文件中指定 key 的位置 配置主機分組,自定義文件,在重新定義一個新的 ansible.cfg 在自

3.1 問題

本案例要求:

給所有主機部署 key
在 inventory 文件中指定 key 的位置
配置主機分組,自定義文件,在重新定義一個新的 ansible.cfg
在自定義的文件夾中完成之前的配置

3.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:給所有主機部署key,案例2已經做過,這裏不再重複

步驟二:在 inventory 文件中指定 key 的位置

[all:vars]
ansible_ssh_private_key_file="/root/.ssh/key"
[root@ansible .ssh]# ansible  all  -m ping    //成功
[root@ansible .ssh]# ssh -i key cache        //不需要輸入密碼,可以直接登陸
Last login: Thu Sep  6 11:49:00 2018 from 192.168.1.40
...
[root@web1 ~]#

步驟三:配置主機分組,自定義文件,在重新定義一個新的 ansible.cfg

自定義的ansible文件只在當前路徑生效

1)自定義文件

[root@ansible ~]# mkdir myansible
[root@ansible ~]# cd myansible/
[root@ansible myansible]# vim myhost
[app1]
web1
db1
[app2]
web2
db2
[app:children]
app1
app2
[other]
cache
[all:vars]
ansible_ssh_private_key_file="/root/.ssh/key"
[root@ansible myansible]# touch ansible.cfg
[root@ansible myansible]# vim ansible.cfg 
[defaults]
inventory = myhost
host_key_checking = False

2)測試結果

[root@ansible myansible]# ansible app1 -m ping  
web1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@ansible myansible]# ansible app -m ping
web1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
db1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
db2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
web2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@ansible myansible]# ansible  app --list-host
  hosts (4):
    web1
    db1
    web2
    db2
[root@ansible myansible]# cd 
[root@ansible ~]# ansible  app1 --list-host   //切換到別的目錄,測試失敗
 [WARNING]: Could not match supplied host pattern, ignoring: app1
 [WARNING]: No hosts matched, nothing to do
  hosts (0):

步驟五:在自定義的文件夾中完成之前的配置,由於步驟都一樣,這裏不再重複

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章