01.Inventory(主机清单)
主机清单配置文件在:
/etc/ansible/hosts
1.1 未分组的主机
1 2 3
| [root@k8s-node2 ~]# vim /etc/ansible/hosts 192.168.56.65 192.168.100.66
|
1.2 属于webservers组主机集合
-
中括号[]里是组名,用于服务器角色分组
-
默认内置两个组:all 和 ungrouped
all
包含所有主机
ungrouped
代表不属于任何组的主机。
- all 和 ungrouped 是隐藏的,不会出现在组列表中。
1 2 3 4
| [root@k8s-node2 ~]# vim /etc/ansible/hosts [webservers] 192.168.56.65 ansible_ssh_user=root ansible_ssh_pass=1 192.168.56.66 ansible_ssh_user=root ansible_ssh_pass=1
|
1.3 主机变量
1 2 3 4 5 6 7 8 9 10
| [root@k8s-node2 ~]# vim /etc/ansible/hosts # 主机变量 [webservers] 192.168.56.65 ansible_ssh_user=root ansible_ssh_pass=1 http_port=80 192.168.56.66 ansible_ssh_user=root ansible_ssh_pass=1
# 组变量 [webservers:vars] http_port=8080 server_name=www.example.com
|
1 2 3 4 5
| [root@k8s-node2 ~]# ansible webservers -a "echo {{http_port}}" 192.168.56.66 | CHANGED | rc=0 >> 8080 192.168.56.65 | CHANGED | rc=0 >> 80
|
04.Ansible基本使用(ad-hoc)
4.1 SSH密码认证
1 2 3
| [webservers] 192.168.56.66:22 ansible_ssh_user=root ansible_ssh_pass=1 192.168.56.65:22 ansible_ssh_user=root ansible_ssh_pass=1
|
4.2 SSH密钥对认证
1 2 3
| [webservers] 192.168.1.10:22 ansible_ssh_user=root ansible_ssh_key=/root/.ssh/id_rsa 192.168.1.11:22 ansible_ssh_user=root
|
4.3 基本命令
1 2 3 4
| [root@k8s-node2 ~]# ansible webservers --list hosts (2): 192.168.56.65 192.168.56.66
|
__END__