ansible的安装可以参考博文ansible安装
ansible可以同时操作属于一个组的多台主机,组和主机之间的关系通过inventory文件来配置,默认的文件路径为/etc/ansible/hosts。
起文件格式如下所示:
[tests]
127.0.0.1
192.168.1.121
方括号[]中是组名,用于对系统进行分类,便于对不同的系统进行个别管理。一台服务器可以属于不同的组。
如果有主机的端口不是标准的22端口,可在主机名之后加上端口号,用冒号分割。ssh配置文件中列出的端口号不会在paramiko连接中使用,会在openssh中使用:
192.168.1.121:5309
也可以设置别名如下:
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
在这个例子中,通过 “jumper” 别名,会连接 192.168.1.50:5555.记住,这是通过 inventory 文件的特×××设置的变量.
主机设置也可以使用范围:
[webservers]
www[01:50].example.com
db-[a:f].test.com
把一个组作为另一个组的子成员,以及分配变量给整个组使用,这些变量可以给/usr/vin/ansible-playbook使用,但是不能给/usr/bin/ansible使用。
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa:children]
southeast
northeast
southwest
northwest
invetory的参数说明:
ansible_ssh_host
将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port
ssh端口号.如果不是默认的端口号,通过此变量设置.
ansible_ssh_user
默认的 ssh 用户名
ansible_ssh_pass
ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_sudo_pass
sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8)
sudo 命令路径(适用于1.8及以上版本)
ansible_connection
与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
ansible_ssh_private_key_file
ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_shell_type
目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
ansible_python_interpreter
目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python
不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).
与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....
转载于:https://blog.51cto.com/zidingyi/1876290