一. 简介
我们在安装主控端安装好ansible之后需要对被控端下发指令进行操作,前面说过现在的ansible的版本是通过ssh连接操作被控端的,在ssh的使用过程中我们是需要交互输入用户和密码的,那么我们在使用ansible的时候应该怎么做呢?
二. 秘钥传递
2.1 --ask-pass
需要手动输入密码。(不建议,避免交互操作)
## ping模块用于测试连通性
[root@clinet test1]# ansible all -m ping --ask-pass
SSH password:
2.2 inventory-密码连接
需要将密码写入到inventory主机清单中(即inventory指定的hosts文件),不建议,密码为明文。
配置hosts文件
# 详细写法
[root@clinet test1]# cat hosts
10.10.10.134 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
10.10.10.135 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[root@clinet test1]#
#省略写法
[root@clinet test1]# cat hosts
10.10.10.[134:135] ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='123'
[root@clinet test1]#
测试联通性
[root@clinet test1]# ansible all -m ping
10.10.10.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.10.10.135 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@clinet test1]#
2.3 免密连接
先创建ssh的密钥对,在将public秘钥传给各个远程主机。(推荐使用)
#创建秘钥对
[root@clinet test1]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
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:DKwLV722/bjWnHLNRmKNENbShe7ToYa0mhkOE9IWeGY root@clinet
The key's randomart image is:
+---[RSA 2048]----+
| . o o. |
| ..E. + + |
| =+... + |
| .o+o .o . . |
| . oo .S. = = . |
| o .o..oo O + |
| . +.=.= B |
| = oo= + |
| .o+.. |
+----[SHA256]-----+
[root@clinet test1]#
# id_rsa和id_rsa_pub分别为私钥和公钥
[root@clinet test1]# ls -l ~/.ssh/
total 12
-rw-------. 1 root root 1679 Oct 11 13:55 id_rsa
-rw-r--r--. 1 root root 393 Oct 11 13:55 id_rsa.pub
-rw-r--r--. 1 root root 348 Oct 11 11:40 known_hosts
[root@clinet test1]#
#将秘钥传递
[root@clinet test1]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@clinet test1]#
三. 普通用户管理被控端
场景说明:ansible使用uos普通用户管理所有的被控端。
3.1 首先控制端被控端都要创建uos用户
[root@clinet test1]#useradd uos
[root@clinet test1]#
[root@clinet test1]#echo "123" | passwd --stdin uos
3.2 将控制端的uos公钥推送到被控端的uos用户下,使用普通用户进行免密
[uost@clinet test1]# ssh-keygen
[uost@clinet test1]#
[uost@clinet test1]# ssh-copy-id -i ~/.ssh/id_rsa.pub uos@ip
3.3 所有主机的uos用户都必须添加sudo权限
[root@clinet test1]#visudo
uos ALL=(ALL) NOPASSWD: ALL
3.4 修改控制端的ansible.config文件,配置普通用户提权
[root@clinet test1]# vim /etc/ansible/ansible.cfg
[privilege_escalation]
become = Ture
become_method = sudo
become_user = root
become_ask_pass = False
四. 常见错误
4.1 ERROR! to use the 'ssh' connection type with passwords, you must install the sshpass program。
完整错误如下:
root@ctnr:/etc/ansible# ansible all -m ping
ctnr.a32-168-1.prod.yiz | FAILED! => { "failed": true, "msg": "ERROR! to use the 'ssh' connection type with passwords, you must install the sshpass program" }
解决方式:
一般出现这种错误,是在通过密码验证远程被管理机的时候,需要在server端安装sshpass:
yum install sshpass -y
4.2 Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.
完整错误如下:
10.10.10.136 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
解决方式:
这种错误通常就出现在server端第一次连接被管理机的时候,就是上面说到的需要通过输入yes/no进行确认将key字符串加入到~/.ssh/known_hosts文件中。
解决办法有两个:
- 通过修改上面提到的host_key_cheking,将其设置为false
- 通过修改ssh_args参数,修改如下:
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
五. 常用指令
5.1 ansible命令
命令ansible是日常工作中使用 的,ansible指令主要就是我们所说的ansible AD-Hoc指令集中使用。如:
## 检查被控端的连通性
# web1只hosts中组为web1所包含的被控端,(后续会着重说明hosts文件的配置)
ansible web1 -m ping
##复制本地文件到远程被控端
ansible web1 -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=root group=root mode=644 backup=yes"
同时ansible的返回结果也很友好,一般分为三种颜色来表示:红色,绿色,橘黄色。其中红色表示执行过程有异常,出现异常的同时会终止剩下的任务。绿色和橘黄色表示执行过程没有异常,所有任务均正常执行。但是橘黄色表示命令执行结束后目标有状态变化,而绿色没有状态变化。
5.2 ansible-galaxy
ansinle-galaxy的功能可以简单的理解为GitHub或pip的功能,通过ansible-galaxy命令,我们可以根据下载量和关注量等信息,查找和安装优秀的Roles。Roles是ansible非常重要的一项功能(后续介绍),在ansible-galaxy上,我们可以上传和下载Roles。(官网地址:https://galaxy.ansible.com)。
# 语法:
ansible-galaxy [init |info |install |list |remove] [--help] [options] ....
ansible-galaxy命令分为三部分:
(1) [init |info |install |list |remove]
init: 初始化本地的roles配置,以备上传roles至galaxy
info: 列表指定role的详细信息;
install:瞎子啊并安装galaxy指定的roles到本地。
list: 列出本地已下载的roles;
remove: 删除本地已下载的roles;
ansible2.0版本开始,针对ansible-galaxy增加了login,import,delete,setup等功能,但是这些功能需要基于login在galaxy认证成功后方可执行,主要为了方便对galaxy上已有的roles的配置工作。
(2)help用法显示[--help]
针对第一部分的init,info等功能,其后跟--help可单独显示该项用法
(3)参数项options
该部分结合第一部的参数完成ansible-galaxy完整的功能
因为ansible-galaxy是对https://galaxy.ansible.com网站的上传下载和配置类工作,所有要确保网站正常访问。
5.3 ansible-pull
该指令的使用涉及ansible的另一种工作模式,pull模式(ansible默认使用push模式)这和通常使用的push模式工作机制恰好相反,其适用于以下场景:①有数量巨大的机器需要配置,即使使用高并发线程依旧花费很多时间;②需要在刚启动的,没有网络连接的主机上运行ansible。
# ansible-pull命令格式如下:
ansible-pull [options] [playbook.yaml]
通过ansible-pul结合Git和crontab一并实现,其原理如下,通过crontab定期拉去指定的Git版本到本地,并以指定模式自动运行预先制定好的指令。
*/20 * * * * root /usr/local/bin/ansible-pull - o -C 2.1.0 -d /srv/www/king-gw/ -i \ /etc/ansible/hosts -U git://git.kingifa.com/king-gw-ansiblepull >> /var/log/ansible-pull.log 2>&1
ansible-pull 通常在配置大批量机器的场景下使用,灵活性稍欠缺,但效率几乎可以无限提升。
5.4 ansible-doc
ansible-doc是ansible模块的文档说明,针对每个模块都有详细的用法和说明以及应用案例介绍,功能和linux系统man命令类似。
语法:
ansible-doc 【options】 【module....】
ansible-doc命令后跟[options]参数或[模块名],显示模块的用法
## 列出所有模块
ansible-doc -l
## 模块功能说明
ansible-doc ping
5.5 ansible-playbook
ansible-playbook是日常应用中使用评率最高的命令,其工作机制是:通过读取预先编写好的playbook文件实现批量管理。需要实现的功能与命令ansible一样,可以理解为按一定条件组成的ansible任务集。
ansoble-playbook命令后跟yml格式的playbook文件,执行事先编写好的任务集,命令如下:
ansible-playbook playbook.yml
playbook具有编写简单,可定制性高,灵活方便,以及可固化日常所有操作的特点。
5.6 ansible-valut
ansible-valut主要用于配置文件加密,如编写好的playbook配置文件中包含敏感信息,不希望被他人看到,可用ansible-vault加密/解密。
语法:
ansible-vault [create |decrypt |edit |encrypt | rekey |view] [--help] [options]
#加密文件
[root@clinet test1]# ansible-vault encrypt xhz.yml
New Vault password:
Confirm New Vault password:
Encryption successful
[root@clinet test1]#
##查看文件乱码
[root@clinet test1]# cat xhz.yml
$ANSIBLE_VAULT;1.1;AES256
34653739616261336263633231623237343161313137623566333164633438653132343232386665
3234653563303334366163636561366164336338316564360a396662396361353762646432366632
30346130333664383030343331346464643536353162626534663264636561386534373733303564
3037323661316635340a303961653835333962396266313837393832323638386639306461366330
3331
[root@clinet test1]#
##解密文件
[root@clinet test1]# ansible-vault decrypt xhz.yml
Vault password:
Decryption successful
[root@clinet test1]# cat xhz.yml
[root@clinet test1]#
5.7 ansible-console
ansible-console是ansible为用户提供的一款交互工具,用户可以在ansible-console虚拟终端上像shell一样使用ansible内置的各种命令。
## ?列出所有模块
[root@clinet test1]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (3)[f:5]$ ?
Documented commands (type help <topic>):
========================================
EOF
a10
a10_server
a10_server_axapi3
a10_service_group
a10_virtual_server
accelerate
aci
## cd 命令到指定的组中
root@all (3)[f:5]$ cd web
root@web (3)[f:5]$ list
10.10.10.134
10.10.10.135
10.10.10.136
root@web (3)[f:5]$
## 设置并发线程
root@web (3)[f:5]$ forks 2
root@web (3)[f:2]$
## 输入yum模块,然后双安tab,列出模块的用法
root@web (3)[f:2]$ yum
allow_downgrade= disable_plugin= exclude= releasever= use_backend=
autoremove= disablerepo= install_weak_deps= security= validate_certs=
bugfix= download_dir= installroot= skip_broken=
conf_file= download_only= list= state=
disable_excludes= enable_plugin= lock_timeout= update_cache=
disable_gpg_check= enablerepo= name= update_only=
root@web (3)[f:2]$ yum