文章目录
一、环境
环境 | 版本 |
---|---|
主机操作系统 | CentOS 7.6.1810 |
Python版本 | 2.7.5 |
控制主机 | 192.168.1.160 |
普通主机 | 192.168.1.170 |
普通主机 | 192.168.1.180 |
普通主机 | 192.168.1.190 |
中文文档:https://ansible-tran.readthedocs.io/en/latest/docs/intro.html
二、yum安装
控制端主机安装 ansible
yum install -y wget \
&& wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo \
&& yum makecache \
&& yum install -y ansible 0:2.9.10-1.el7
查看版本
[root@localhost ~]# ansible --version
ansible 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
三、添加主机列表
安装 vim
编辑器
yum install -y vim
3.1 禁用公钥认证
编辑 /etc/ansible/ansible.cfg
配置文件,取消 host_key_checking
注释
[defaults]
host_key_checking = False
如图
3.2 主机与主机组
Ansible
可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory
文件配置. 默认的文件路径为 /etc/ansible/hosts
[Nginx]
127.0.0.1 ansible_connection=local ansible_ssh_user=root ansible_ssh_pass=root160
[Java]
192.168.1.170 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=root170
192.168.1.180 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=root180
[Mysql]
192.168.1.190 ansible_connection=ssh ansible_ssh_user=root ansible_ssh_pass=root190
参数说明
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 的路径....
四、连通性测试
命令格式如下:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
4.1 测试Mysql组
[root@localhost ~]# ansible Mysql -m ping
192.168.1.190 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.2 测试Java组
[root@localhost ~]# ansible Java -m ping
192.168.1.170 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.180 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.3 测试192.168.1.180单个主机
[root@localhost ~]# ansible 192.168.1.180 -m ping
192.168.1.180 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4.4 测试所有组
[root@localhost ~]# ansible all -m ping
127.0.0.1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.180 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.170 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.1.190 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}