- 編輯/etc/ansible/hosts
- 添加本機的public SSH key到目標機器的authorized_keys #ssh-copy-id
- 添加本機的私鑰到Ansible
- 運行ansible all -m ping 測試是否添加成功
Inventory 分組
Ansible可同時操作屬於一個組的多台主機,組和主機之間的關系通過inventory文件配置,默認文件路徑為/etc/ansible/hosts
常用參數配置:
ansible_ssh_host # 目標主機地址
ansible_ssh_port # 目標主機端口,默認22
ansible_ssh_user # 目標主機用戶
ansible_ssh_pass # 目標主機ssh密碼
ansible_sudo_pass # sudo密碼
ansible_sudo_exe
ansible_connection # 與主機的連接類型,比如:local,ssh或者paramiko
ansible_ssh_private_key_file # 私鑰地址
ansible_shell_type # 目標系統的shell類型
ansible_python_interpreter # python版本
格式:[組名]
例如 :
[test] # 組名
172.18.0.11 #
主機ip 或者1172.18.0.11:65522 自定義端口(不指明,則使用ansible.cfg文件中“remote_port ”定義的端口,如果沒有定義則使用默認的22端口)
別名
s1 ansible_ssh_port=65522 ansible_ssh_host=10.0.0.1 ansible_ssh_user=simon # 別名s1
連續的主機
[g1]
g[1:50].example.com
g[a-f].example.com
hosts簡單配置實例
[all:vars]
# *:vars 塊變量,all:vars 全局變量
ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_port=22
ansible_ssh_user=root
[t3:vars]
# t3 使用python解釋器是python2
ansible_python_interpreter=/usr/bin/python2
nginx_port=80
# 私有變量在playbooks中使用
[t3]
192.168.11.162
Inventory 分文件管理(文件名即是組名)
分文件:
/etc/ansible/group_vars/
vim
/etc/ansible/group_vars/
webservers
/etc/ansible/host_vars/
foosball
vim 文件中的內容: 變量
ntp_server:
acme.example.org
db_server: 10.0.0.1
分文件夾:(文件夾就是組名)
/etc/ansible/group_vars/
vim/install
/etc/ansible/group_vars/
vim/init
/etc/ansible/host_vars/foosball