Ansible是一种IT自动化工具。它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部署,滚动更新。Ansible适用于管理企业IT基础设施,从具有少数主机的小规模到数千个实例的企业环境。Ansible也是一种简单的自动化语言,可以完美地描述IT应用程序基础结构。
具备以下三个特点:
简单:减少学习成本
强大:协调应用程序生命周期
Modules:任务均有模块完成,也可以自定义模块,例如经常用的脚本。
Plugins:使用插件增加Ansible核心功能,自身提供了很多插件,也可以自定义插件。例如connection插件,用于连接目标主机。
Ansible hosts文件写法
hosts文件写法规范类型主要有两种:
如果端口都一样可以去/etc/ansible.cfg文件里修改端口,这里可以不用添加
web01-172.16.1.7..都是别名
- 分组配置主机清单使用密码的情况
[test]
web01-172.16.1.7 ansible_ssh_host=172.16.1.7 ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
web02-172.16.1.8 ansible_ssh_host=172.16.1.8 ansbile_ssh_user="root" ansible_ssh_pass="1234" ansible
_ssh_port=22
web03-172.16.1.9 ansible_ssh_host=172.16.1.9 ansible_ssh_user="root" ansible_ssh_pass="123" ansible_
ssh_port=22
- 分组配置主机清单使用密钥的情况
[xxx] web01-172.16.1.7 ansible_ssh_host=172.16.1.7 ansible_ssh_port=22 web02-172.16.1.8 ansible_ssh_host=172.16.1.8 ansible_ssh_port=22 web03-172.16.1.9 ansible_ssh_host=172.16.1.9 ansible_ssh_port=22
- 分组配置主机清单使用练习IP地址比如[7:10]代表7,8,9,10四台主机
[xxx] web ansible_ssh_host=172.16.1.[7:10] ansible_ssh_port=22 nginx ansible_ssh_host=172.16.1.[1:3] ansible_ssh_port=22 mysql ansible_ssh_host=172.16.1.[4:6] ansible_ssh_port=22
- 嵌入式配置主机清单
[xxx:children] xxx_server xxx_clinet [xxx_server] web01-172.16.1.7 ansible_ssh_host=172.16.1.7 ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22 web02-172.16.1.8 ansible_ssh_host=172.16.1.8 ansbile_ssh_user="root" ansible_ssh_pass="1234" ansible _ssh_port=22 web03-172.16.1.9 ansible_ssh_host=172.16.1.9 ansible_ssh_user="root" ansible_ssh_pass="123" ansible_ ssh_port=22 [xxx_clinet] web ansible_ssh_host=172.16.1.[7:10] ansible_ssh_port=22 nginx ansible_ssh_host=172.16.1.[1:3] ansible_ssh_port=22 mysql ansible_ssh_host=172.16.1.[4:6] ansible_ssh_port=22
Ansible 配置文件
/etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
#forks = 5
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#remote_port = 22
#log_path = /var/log/ansible.log
inventory = /etc/ansible/hosts 主机清单
#library = /usr/share/my_modules/ 库文件存放位置
#remote_tmp = ~/.ansible/tmp 临时py文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp 本机的临时执行目录
#forks = 5 默认并发数
#sudo_user = root 默认sudo用户
#ask_sudo_pass = True 每次执行是否询问sudo的ssh密码
#ask_pass = True 每次执行是否询问ssh密码
#remote_port = 22 远程主机端口
#log_path = /var/log/ansible.log ansible的日志文件
- host_key_checking = False # 首次连接是否需要检查key认证,建议设为False
sudo用户提权使用ansible
1.修改/etc/ansible/ansible.cfg
[privilege_escalation]
become=True
become_method=sudo
2.hostss中添加一个sudo密码
ansible_ssh_pass=xxxxx ansible_ssh_user=hwf ansible_become_pass=xxxx
3.你需要使用提权命令的前面加上 sudo

评论