内容介绍
一、ansible的两种运行模式
二、ad-hoc的使用场景
三、用命令来看两种模式的运行不同
四、ansible的配置文件
五、lnventory配置文件
六、Inventory配置
七、ansible配置文件
八、重要配置项目
一、ansible的两种运行模式
- ansible提供了两种运行模式: ad-hoc和playbook
- ad-hoc模式提供了单次运行的可能,如果你只是临时执行命令,不需要持久化,就可以使用 ad-hoc
- playbook 模式提供了重复执行复杂任务的可能,对于复杂或需要重复执行的任务,都应设置为playbook
二、ad-hoc的使用场景
1.在多台机器上,查看某个进程是否启动
2.从多台机器上,拷贝指定日志文件到本地
三、用命令来看两种模式的运行不同
ad-hoc模式: ansible prod -m command -a 'ls /root'
playbook模式: ansible-playbook playbook.yml
四、ansible的配置文件
- ansible的配置文件分为两类:
- Inventory :主机配置文件,用于指明所有可以使用的主机
- 标准配置文件:用于指明ansible 的各项特征的配置文件。
五、lnventory配置文件
Inventory中纪录了不同的主机信息,可以用于批量管理服务器:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbsevers]
one.eaxmple.com
two.eaxmple.com
three.eaxmple.com
特殊的Inventory
当你的SSH没有使用默认配置时,可以通过参数进行配置
badwolf.example.com:5309 #自定义端口
[webservers] #批量设定主机
www[01:50].example.com
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_ssh_user = mpdehaan
other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan
www[01:50].example.com
www01.example.com
www02.example.com
…
www50.example.com
六、Inventory配置
- ansible_ssh_host将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
- ansible_ssh_port ssh端口号.如果不是默认的端口号,通过此变量设置.
- ansible_ssh_user 默认的ssh 用户名
- ansible_ssh_pass ssh密码
- ansible_sudo_pass sudo密码
- ansible_connection与主机的连接类型.比如:local, ssh 或者
- ansible_ssh_private_key_file ssh使用的私钥文件.适用于有多个密钥的场景
- ansible_shell_type目标系统的shell 类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
- ansible_python_interpreter目标主机的python路径.适用于的情况:系统中有多个Python,或者命令路径不是"/usr/bin/python
七、nsible配置文件
ansible配置文件指明了ansible 的表现
- ansible配置文件
- ANSIBLE_CONFIG(一个环境变量)
- cfg(位于当前目录中)
- .ansible.cfg (位于home目录中)-\
- /etc/ansible/ansible.cfg
八、重要配置项目
- ask_pass or ask_sudo_pass︰是否请求用户密码或请求sudo密码inventory :配置Inventory文件路径
- log_path: 日志文件路径
- module_name : ad-hoc模式下使用的默认模块
- .hosts : playbook模式下的默认主机,默认值为 * ,即所有主机.
- remote_port: 远程主机的默认端口,默认值为22
- remote_user: 远程主机的默认用户,默认值为当前用户
- forks : 单次执行的并行进程数,根据实际主机的数量进行调整
多台设备怎么设置批量配置免密
- Cloudinit:安装完成后进行初始化
- 镜像修改