前言
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
概念
Ansible可同时操作属于一个组的多台主机,组和主机之间的关系通过Inventory文件配置(vim /etc/ansible/ansible.cfg
)默认的文件路径为 /etc/ansible/hosts。
组成部分
例:
one.example.com
[tupb]
two.example.com
three.example.com
方括号[ ] 是组名,一个主机可以属于不同的组,比如一台服务器可以属于tupb组和tupb01组。这时可以指定tupb组和tupb02组,都可以操作这台主机。
ansible tupb -a ‘ls’
ansible tupb01 -a ‘ls’
ansible 192.168.1.2 -a ‘ls’
如何自定义链接端口
编辑host文件,在域名或者ip后,加上冒号和端口号
192.168.1.2:8080
one.example.com:8080
IP域名太长,起别名
jumper ansible_ssh_port=8080 ansible_ssh_host=192.168.1.2
不以root用户登录
jumper ansible_ssh_port=8080 ansible_ssh_host=192.168.1.2 ansible_ssh_user=tupb
机器太多,但是连续的,写起来比较麻烦
[tupb02]
tupb[1:20].example.com
tupb[a-h].example.com
Inventory 参数选项
ansible_ssh_host
ansible_ssh_prot
ansible_ssh_user
ansible_ssh_pass ssh 密码
ansible_sudo_pass sudo 密码
ansible_sudo_exe
ansible_connection 与主机的连接类型。eg:ssh;local;paramiko
ansible_ssh_pravate_key_file
ansible_shell_type 目标系统的shell类型
ansible_python_interpreter
分文件管理
分文件——例如
/etc/ansible/group_vars/tupb/install
/etc/ansible/group_vars/tupb/init
/etc/ansible/group_vars/tupb/foot
install 文件中的内容:
ntp_server:tupb.example.org
database_server:storage.example.org
这时候文件夹名就是组名。