第二章 Ansible服务搭建

一、环境准备

主机名 IP 身份
m01 10.0.0.61 Ansible 控制端
web01 172.16.1.7 Ansible 被控端
web03 172.16.1.9 Ansible 被控端

二、安装Ansible

[root@m01 ~]# yum install -y ansible

三、Ansible命令详解

# ansible <host-pattern> [options]
--version   #ansible版本信息
-i          #主机清单文件路径,默认是在/etc/ansible/hosts
-m          #使用的模块名称,默认使用command模块
-a          #使用的模块参数,模块的具体动作
-k          #提示输入ssh密码,而不使用基于ssh的密钥认证
-C          #模拟执行测试,但不会真的执行
-T          #执行命令的超时

#查看Ansible版本及模块路径
[root@m01 ~]# 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)]

四、Ansible配置文件读取顺序

[root@m01 ~]# vim /etc/ansible/ansible.cfg 
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[root@m01 ~]# rpm -ql ansible
[root@m01 ~]# zcat /usr/share/man/man1/ansible-config.1.gz

#要查看完整列表,请访问https://docs.ansibe.com/或使用ansibe-config命令。
For a full list check \fI\%https://docs.ansible.com/\fP\&. or use the \fIansible\-config\fP command.

#/etc/ansible/ansible.cfg 配置文件,如果存在则使用
/etc/ansible/ansible.cfg \-\- Config file, used if present

#~/.ansible.cfg 用户配置文件,覆盖默认配置(如果存在)
~/.ansible.cfg \-\- User config file, overrides the default config if present

#\&/ansible.cfg 本地配置文件(在当前工作目录中)假定为(aqproject-specific)(aq,如果存在,则重写其余文件)。
\&./ansible.cfg \-\- Local config file (in current working directory) assumed to be \(aqproject specific\(aq and overrides the rest if present.

#如上所述,ANSIBLE_CONFIG环境变量将覆盖所有其他环境变量。
As mentioned above, the ANSIBLE_CONFIG environment variable will override all others.

#生效优先级
ANSIBLE_CONFIG  >>  $ANSIBLE_CONFIG/ansible.cfg  >>  ~/.ansible.cfg  >>  /etc/ansible/ansible.cfg

五、配置Ansible

[root@m01 ~]# cat /etc/ansible/ansible.cfg 
#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                      #远程主机端口
host_key_checking = False                 #跳过检查主机指纹
log_path = /var/log/ansible.log           #ansible日志

#普通用户提权操作
[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False 

六、配置主机清单

1.单主机配置方式一

#ip+端口+用户+密码
[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
172.16.1.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
[web03]
172.16.1.7 ansible_ssh_port=22

#测试主机清单
[root@m01 ~]# ansible '*' -m ping

2.单主机配置方式二

[root@m01 ~]# vim /etc/hosts
172.16.1.7 web01
172.16.1.9 web03

[root@m01 ~]# vim /etc/ansible/hosts 
[web01]
web01 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'
[web03]
web03 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1'

3.单主机配置方式三

[root@m01 ~]# vim /etc/ansible/hosts 
[web1]
web01 
[web3]
web03
[web1:vars]
ansible_ssh_pass='1'
[web3:vars]
ansible_ssh_pass='1'

4.单主机配置基于秘钥方式

#生成密钥对
[root@m01 ~]# ssh-keygen

#推送秘钥
[root@m01 ~]# ssh-copy-id 172.16.1.7
[root@m01 ~]# ssh-copy-id 172.16.1.9

#配置
[root@m01 ~]# vim /etc/ansible/hosts 
[web1]
web01
[web3]
web03

5.单主机主机组配置

[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01
web03
[nfs_group]
nfs ansible_ssh_pass='1'

#查看组下面的机器
[root@m01 ~]# ansible 'web_group' --list-host
  hosts (2):
    web01
    web03

6.单主机操作多个组

[root@m01 ~]# ansible 'web_group,nfs_group' -m ping

定义包含组
[root@m01 ~]# vim /etc/ansible/hosts 
[web_group]
web01
web03

[nfs_group]
nfs ansible_ssh_pass='1'

[mysql_group]
db01 ansible_ssh_pass='1'
db02 ansible_ssh_pass='1'

[nfs_zu:children]
web_group
nfs_group

#注意:
	主机名和组名称不要一样,否则执行命令时不知道找主机还是主机组
posted @ 2020-09-23 19:01  年少纵马且长歌  阅读(291)  评论(0编辑  收藏  举报