ansible的简单使用

安装Ansible

1.环境准备

主机名 wan ip lan ip 角色
m01 10.0.0.61 172.16.1.61 ansible控制端
web01 10.0.0.7 172.16.1.7 ansible被控端
web02 10.0.0.8 172.16.1.8 ansible被控端

2.安装ansible

#卸载saltmaster
[root@m01 ~]# yum remove -y salt-master salt-minion

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

#查看ansible版本和相关信息
[root@m01 ~]# ansible --version
  config file = /etc/ansible/ansible.cfg	#主配置文件
  python version = 2.7.5
  
# 查看默认主机清单位置及相关
[root@m01 ~]# vim /etc/ansible/ansible.cfg

[root@m01 ~]# rpm -q ansible
ansible-2.9.9-1.el7.noarch
[root@m01 ~]# rpm -ql ansible|grep -v lib
/etc/ansible/ansible.cfg	#配置文件
/etc/ansible/hosts		#ansible默认主机清单文件
/etc/ansible/roles
/usr/bin/ansible	#在全局环境变量里面,可以TAB和使用相对路径执行
/usr/bin/ansible-2
/usr/share/ansible

-- version	#ansible版本信息,检查当前使用的是哪个配置文件
-i 			#指定主机清单的路径(默认指定/etc/ansible/hosts)

-m			#使用的模块名称,默认使用command模块(不支持特殊符号,可省略),还有shell模块(支持特殊符号,不过也不太好用)
-a			#使用的模块参数,模块的具体动作

-k			#提示输入ssh密码,而不是基于ssh的密匙认证
-C			#模拟执行测试,但是不会真的执行(编译)
-T			#执行命令超时时间

ansible配置文件优先级

1.$ANSIBLE_CONFIG	#ansible相关的环境变量
2../ansible.cfg		#当前工作目录下的
3.~/ansible.cfg		#当前用户家目录下的
4./etc/ansible/ansible.cfg	#主配置文件

#文件名必须是ansible.cfg,可以使用-i切换配置文件

ansible配置文件及优化

[root@m01 ~]#  vim /etc/ansible/ansible.cfg		
[defaults]
#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
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5			#默认并发数
#poll_interval  = 15
#sudo_user      = root		#默认sudo用户
#ask_sudo_pass = True		#每次执行是否询问sudo的ssh密码
#ask_pass      = True		#每次执行是否询问ssh密码
#transport      = smart
#remote_port    = 22		#远程主机端口
#module_lang    = C
#module_set_locale = False	#是否跳过检查的主机指纹(与第一次连接时的yes/no有关)

#log_path = /var/log/ansible.log	#ansible日志

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

#跳过检查的主机指纹,
不优化就不能直接使用ansible远程连接,所以服务端一定要做
(不做的话就会显示know_hosts报错,不打开的话也可以,不过要先使用ssh挨个连接一遍,才能使用ansible来管理客户端)
优化后可以直接使用ansible远程连接
[root@m01 ~]#  vim /etc/ansible/ansible.cfg		#改为
host_key_checking = False

ansible inventory(主机清单)

/etc/ansible/hosts 是ansible默认主机资产清单文件,用于定义被管理主机的认证信息,例如ssh登录用户名,密码以及key信息

inventory文件中填写需要被管理的主机与主机组信息,还可以自定义inventory主机清单的位置,使用 -i 指定文件位置即可

ansible的连接可以使用本地的缓存

使用场景一

[root@m01 ~]# vim /etc/ansible/hosts 
#[]标签名任意,但是最好不要用特殊符号和大写字母,中文
#端口是22的时候可以省略
[web_group]
172.16.1.7 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.8 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.9 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[db_group]
172.16.1.51 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.52 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.53 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.54 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[nfs_group]
172.16.1.31 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[redis_group]
172.16.1.81 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[lb_group]
172.16.1.5 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
172.16.1.6 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[backup_group]
172.16.1.41 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[zabbix_group]
172.16.1.71 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[m01_group]
172.16.1.61 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

[mtj_group]
172.16.1.202 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

使用场景二

支持正则

#主机名(相当于注释)不等于主机名,ansible命令指定的主机名是在hosts文件中的主机名
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web0[1:2:3] ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

#需要做本地域名解析
[root@m01 ~]# vim /etc/hosts
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03

#主机名(相当于注释)不等于主机名
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host='172.16.1.7' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
web02 ansible_ssh_host='172.16.1.8' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
web03 ansible_ssh_host='172.16.1.9' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'

使用场景三(变量)

使用变量指定密码

[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8
web03 ansible_ssh_host=172.16.1.9

[db_group]
db01 ansible_ssh_host=172.16.1.5
db02 ansible_ssh_host=172.16.1.6

#指定web_group组的主机密码,等于把密码加入到主机组
[web_group:vars]
ansible_ssh_pass='1'

[db_group:vars]
ansible_ssh_pass='1'

使用场景四(变量)

#自定义标签组或者(合并组),新组名为install_rsync和install_nfs
[root@m01 ~]# vim /etc/ansible/hosts
[install_rsync:children]
web_group
nfs_group

[install_nfs:children]
web_group
nfs_group

使用场景五(秘钥)

通过密匙认证客户端

1.创建密钥对
[root@m01 ~]# ssh-keygen
2.推送公钥
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.52
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.53
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.54
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61

3.编辑主机清单
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22
web03 ansible_ssh_host=172.16.1.9 asible_ssh_user=root ansible_ssh_port=22

模块是Ansible执行的最小单位,可以是由Python编写,也可以是Shell编写,也可以是由其他语言编写。

#检查服务端和客户端是否可以连通(是真正可以连接的,受控端状态异常的话报错)
只能指定标签名,不能指定主机名
[root@m01 ~]# ansible 'web_group' -m ping
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"	#缓存和使用命令
    }, 
    "changed": false, 
    "ping": "pong"		#ping pong 相当于模块之间的暗号
}
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.9 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

#查看可以远程连接的主机(不是真正可以连接的,只是显示本地记录的)
[root@m01 ~]# ansible 'all' --list-host
  hosts (11):
    172.16.1.5
    172.16.1.6
    172.16.1.31
    172.16.1.7
    172.16.1.8
    172.16.1.9
    172.16.1.51
    172.16.1.52
    172.16.1.53
    172.16.1.54
    172.16.1.81
[root@m01 ~]# ansible 'web_group' --list-host
[root@m01 ~]# ansible 'web01' --list-host
[root@m01 ~]# ansible '10.0.0.7' --list-host
[root@m01 ~]# ansible 'all' --list-host
[root@m01 ~]# ansible 'lnmp' --list-host
[root@m01 ~]# ansible '*' --list-host	#显示的是默认主机清单中的主机
  hosts (11):
    172.16.1.5
    172.16.1.6
    172.16.1.31
    172.16.1.7
    172.16.1.8
    172.16.1.9
    172.16.1.51
    172.16.1.52
    172.16.1.53
    172.16.1.54
    172.16.1.81

# -i指定自定义主机清单,自定义操作(一般用于临时批量操作)
# --list-host只显示主机清单中的第一列
[root@m01 ~]# ansible '*' -i /root/hosts --list-host
  hosts (5):
    web01
    web02
    web03
    db01
    db02

ansible ad-hoc (ansible执行的临时命令)

ansible-doc (帮助命令)

ansible-playbook(ansible执行命令的剧本)

#查看模块语法
[root@m01 ~]# ansible-doc command
EXAMPLES:

#查看支持的模块个数
[root@localhost ~]# ansible-doc -l |wc -l   

[root@m01 ~]# which ansible-doc
/usr/bin/ansible-doc

ansible-hoc结构返回的颜色

绿色:表示被管理端主机没有被控制端修改(执行成功)

黄色:表示被管理端主机发生变更(执行成功)

红色:表示出现了故障,注意看提示

粉色:警告(说明命令的执行有可能行,有可能不行)

ansible使用yml语法(playbook)

#卸载httpd
[root@m01 ~]# yum remove -y httpd

#编辑yml语法(小心2468空格,不能直接TAB)
[root@m01 ~]# vim httpd.yml
- hosts: web_group
  tasks:
    - name: Install Httpd Server	#随便写,只是个标记
      yum:
        name: httpd
        state: present
        
#检查yml语法        
[root@m01 ~]# ansible-playbook --syntax-check httpd.yml
playbook: httpd.yml

#控制端执行
1.工作目录清单优先级 > 家目录下清单优先级 > 默认主机清单优先级(?)
2. -C 指定要执行的yml语法文件
[root@m01 ~]# ansible-playbook -C httpd.yml 
PLAY [web_group] ******************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [172.16.1.8]
ok: [172.16.1.7]
ok: [172.16.1.9]
原文地址:https://www.cnblogs.com/syy1757528181/p/13069028.html