安装ansible
[root@localhost ~]# yum install epel-release.noarch -y
[root@localhost ~]# yum install -y ansible
免密登录
# 三台机器设置hosts解析,方便之后用域名管理
[root@server1 ~]# vim /etc/hosts
192.168.175.10 server1
192.168.175.20 server2
192.168.175.30 server3
# 生成密钥对
[root@server1 ~]# ssh-keygen -P "" -t rsa
# 将公钥发送给需要被管理端,需要输入对方的免密
[root@server1 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@server1
[root@server1 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@server2
[root@server1 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@server3
常用命令:
ansible:临时命令执行工具,常用于执行临时命令
ansible-doc:常用于模块功能的查询
ansible-playbook:用于执行剧本
常见配置文件:
/etc/ansible/ansible.cfg:主配置文件
/etc/ansible/hosts:主机清单文件
/etc/ansible/roles:角色目录
修改配置文件
#ansible host配置文件
[root@localhost ~]# cat /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers] #中括号代表一个组,下面的域名或者ip表示组里的成员
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
# Here's another example of host ranges, this time there are no
# leading 0s:
## db-[99:101]-node.example.com
#定义自己的组
[all-servers]
server1
server2
server3
[node1]
server2
[node2]
server3
#ansible.cfg文件,默认可以不用改
[root@localhost ~]# vim /etc/ansible/ansible.cfg
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts
# 定义主机清单文件
#library = /usr/share/my_modules/ # 库
文件的存放位置
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
# 生成的临时py文件在远程主机的目录
#local_tmp = ~/.ansible/tmp
# 生成的临时py文件在本地主机的目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml #
#forks = 5 # 默认的并发数
#poll_interval = 15 # 默认的线程池
#sudo_user = root # 默认的sudo用户
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False
ansible参数说明
-a MODULE_ARGS:指定模块的参数
-m MODULE_NAME:指定模块
-C:检查执行结果
-e EXTRA_VARS:指明变量名
-f FORKS:指定并发进程数
-i INVENTORY:指定主机清单文件
--syntax-check:检查执行命令是否存在语法错误