ansible环境部署
ansible简介
1.Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Window主机。管理节点的只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansible。
2.ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、 chef、func、fabric) 的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
3.ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
a.连接插件connection plugins:负责和被监控端实现通信(远程连接通过ssh端口22)
b.host inventory:指定操作的主机,是一个配置文件里面定义监控的主机
c.各种模块核心模块,command模块,自定义模块
d.借助于插件完成记录日志邮件等功能
e.playbook: 剧本执行多个任务时,非必需可以让节点一次性运行多个任务
4.ansible的架构:连接其它主机默认使用ssh协议
ansible环境部署
安装epel源
yum -y install epel-release
yum -y install ansible
ansible --version
安装树状结构来查看文件
[root@master ~]# yum -y install tree
[root@master ~]# tree /etc/ansible
/etc/ansible
├── ansible.cfg ansible的配置文件
├── hosts ansible的主仓库,用于存储需要管理的远程主机的相关信息,主机清单
└── roles 角色
配置主机清单
[root@master ~]# vi /etc/ansible/hosts
[webserver]
20.0.0.11
[mysql]
20.0.0.12
配置免交互的密钥对验证,建立远程连接
[root@master ~]# ssh-keygen -t rsa 生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 生成密钥存储位置
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 对密钥设置密码加密,密码abc123
Enter same passphrase again: 再次确认密码abc123
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vBwgnptNmeeOrnbTOrwMOfybBwlcdyHRBZZq+v1IUd0 root@master
The key's randomart image is:
+---[RSA 2048]----+
| oo+=. |
| . .o+ . . |
| ..... o . . E|
| .oo =o . |
| o.++S . |
| . *++ o . |
| B..++.. |
| .==++... |
| ..+XB. ... |
+----[SHA256]-----+
[root@master ~]# ls -a
[root@master ~]# cd .ssh/
[root@master .ssh]# ls 查看生成的密钥
id_rsa (私钥)id_rsa.pub(公钥)
ansible命令行模块
ansible 主机标识/IP地址 -m 模块 -a ‘参数’
command命令模块
ansible 192.168.17.30 -m command -a 'date'
cron计划任务模块
ansible sql -m cron -a 'minute="*/1" job="/usr/bin/echo hello >> /opt/hello.txt" name="cron_hello"'
user模块
ansible all -m user -a 'name="tom"'
group模块
[root@master .ssh]# ansible mysql -m group -a 'name=apple gid=1050 system=yes'
copy模块
ansible mysql -m copy -a 'content="this is a pingguo" dest=/opt/pingguo.txt'
file模块
ansible mysql -m file -a 'owner=root group=tree mode=755 path=/opt/fstab.bak'
ping模块
ansible all -m ping
yun模块
ansible webserver -m yum -a 'name=httpd'
service模块
ansible webserver -m service -a 'name=firewalld state=stopped'
setup模块
ansible webserver -m setup #获取webserver组主机的facts信息
注:
ansible_ssh_host 将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port ssh端口号.如果不是默认的端口号,通过此变量设置. ansible_ssh_user 默认的 ssh
用户名 ansible_ssh_pass ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_ssh_private_key_file ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_ssh_common_args 此设置附加到sftp,scp和ssh的缺省命令行
ansible_sftp_extra_args 此设置附加到默认sftp命令行。
ansible_scp_extra_args 此设置附加到默认scp命令行。
ansible_ssh_extra_args 此设置附加到默认ssh命令行。
ansible_ssh_pipelining 确定是否使用SSH管道。 这可以覆盖ansible.cfg中得设置。
ansible_shell_type 目标系统的shell类型.默认情况下,命令的执行使用 ‘sh’ 语法,可设置为 ‘csh’ 或
‘fish’. ansible_python_interpreter 目标主机的 python 路径.适用于的情况: 系统中有多个
Python, 或者命令路径不是"/usr/bin/python",比如 BSD, 或者 /usr/bin/python
ansible__interpreter 这里的"*"可以是ruby 或perl
或其他语言的解释器,作用和ansible_python_interpreter 类似
ansible_shell_executable 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh。