ansible安装及配置(一)

第1章 ansible介绍

1.1 ansible软件知识介绍

  • ansible是一个基于Python开发的自动化运维工具
  • ansible的功能实现基于SSH远程连接服务
  • ansible可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能

说明:ansible软件相关参考链接信息

http://docs.ansible.com/ansible/intro_installation.html

http://www.ansible.com.cn/

http://docs.ansible.com/modules_by_category.html

http://www.ansible.cn/docs/

1.2 ansible软件特点

  • 不需要单独安装客户端,基于系统自带的sshd服务,sshd就相当于ansible的客户端
  • 不需要服务端
  • 需要依靠大量的模块实现批量管理
  • 配置文件/etc/ansible/ansible.cfg

第2章 ansible部署安装

2.1 环境准备

2.1.1 主机架构

服务器说明 外网IP 内网IP 主机名
nginx web 10.0.0.8/24 172.16.1.8/24 web01
NFS存储服务器 10.0.0.31/24 172.16.1.31/24 nfs01
rsync备份服务器 10.0.0.41/24 172.16.1.41/24 backup
管理服务器 10.0.0.61/24 172.16.1.61/24 m01

2.1.2 主机架构图

2.1.3 主机之间已分发密钥

[root@m01 ansible]# ssh-keygen
[root@m01 ansible]# sshpass -p123456 ssh-copy-id -i ~/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@backup"
Now try logging into the machine, with "ssh '-o StrictHostKeyChecking=no root@backup'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@m01 ansible]# sshpass -p123456 ssh-copy-id -i ~/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@nfs01"
Warning: Permanently added 'nfs01' (RSA) to the list of known hosts.
Now try logging into the machine, with "ssh '-o StrictHostKeyChecking=no root@nfs01'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@m01 ansible]# sshpass -p123456 ssh-copy-id -i ~/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@web01"
Now try logging into the machine, with "ssh '-o StrictHostKeyChecking=no root@web01'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

2.2 安装ansible

提示:搭建企业yum仓库及定制rpm包是自动化运维关键内容。

2.2.1 保留yum安装的软件

[root@m01 ansible]# sed -i.bak 's#keepcache=0#keepcache=1#g' /etc/yum.conf
[root@m01 ansible]# grep keepcache /etc/yum.conf
keepcache=1

2.2.2 管理端m01安装ansible

#更新epel源
[root@m01 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
#安装ansible
[root@m01 ~]# yum install -y ansible
...
Installed:
  ansible.noarch 0:2.3.1.0-1.el6                                                             

Dependency Installed:
  PyYAML.x86_64 0:3.10-3.1.el6                libyaml.x86_64 0:0.1.3-4.el6_6                 p
  python-httplib2.noarch 0:0.7.7-1.el6        python-jinja2-26.noarch 0:2.6-3.el6            p
  python-pyasn1.noarch 0:0.0.12a-1.el6        python-setuptools.noarch 0:0.6.10-3.el6        p

Complete!

提示:ansible安装需要epel.repo源。

2.2.3 所有被管理端安装

[root@backup ~]# yum install -y libselinux-python
...
Installed:
  libselinux-python.x86_64 0:2.0.94-7.el6                                                    

Complete!

2.2.4 查看安装的软件信息

[root@m01 ansible]# rpm -ql ansible | wc -l
4874            #文件较多
[root@m01 ~]# rpm -ql ansible | egrep -v "/usr/share|/usr/lib"
/etc/ansible
/etc/ansible/ansible.cfg            #ansible配置文件
/etc/ansible/hosts                  #定义ansible可以管理的主机信息
/etc/ansible/roles                  #主要在自动化部署多台主机时应用
/usr/bin/ansible
/usr/bin/ansible-2
/usr/bin/ansible-2.6
/usr/bin/ansible-connection
/usr/bin/ansible-console
/usr/bin/ansible-console-2
/usr/bin/ansible-console-2.6
/usr/bin/ansible-doc
/usr/bin/ansible-doc-2
/usr/bin/ansible-doc-2.6
/usr/bin/ansible-galaxy
/usr/bin/ansible-galaxy-2
/usr/bin/ansible-galaxy-2.6
/usr/bin/ansible-playbook           #执行ansible剧本命令
/usr/bin/ansible-playbook-2
/usr/bin/ansible-playbook-2.6
/usr/bin/ansible-pull
/usr/bin/ansible-pull-2
/usr/bin/ansible-pull-2.6
/usr/bin/ansible-vault
/usr/bin/ansible-vault-2
/usr/bin/ansible-vault-2.6
[root@m01 ansible]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg                  #ansible配置
├── hosts                        #被ansible管理的主机名单(分组)
└── roles

1 directory, 2 files

第3章 配置ansible

3.1 ansible基础配置

3.1.1 基于SSH密钥登录的配置方式

[root@m01 ~]# cp /etc/ansible/hosts{,.bak} #更改配置文件前备份可是个好习惯
[root@m01 ~]# vim /etc/ansible/hosts
...
[test]                #添加主机组名
172.16.1.31             #添加主机IP
172.16.1.41
172.16.1.61
172.16.1.8

3.1.2 基于SSH密码登录的配置方式

ansible软件服务部署的前提是ssh+key免密码环境已经部署,而在没有进行部署ssh+key环境下,如果想使用ansible也可以用以下方式配置hosts文件,实现ansible批量管理功能。

[root@m01 ~]# tail -6 /etc/ansible/hosts
[test]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.41 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.61 ansible_ssh_user=root ansible_ssh_pass=登录密码
172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=登录密码
  • 命令说明:

后面的用户和密码是非必须的,在配置key认证的情况下,不使用密码也可以直接操作,未使用key的,也可以在ansible通过-k参数在操作前询问手动输入密码。

  1. ansible_ssh_user:ssh连接的用户名
  2. ansible_ssh_pass:ssh连接的密码

提示:如果没有做密钥认证,hosts又没有如上方式配置的话,ansible进行远程连接是会失败的。

第4章 ansible远程批量执行命令

4.1 语法格式:

ansible [主机组] -m [ansible内置功能模块名] -a [命令]
ansible test -m command -a 'uptime'

4.1.1 语法图解

图片[1]|ansible安装及配置(一)|leon的博客

4.2 ansible软件常用参数表

命令参数 参数说明
-m NAME –module-name=NAME

Execute the module called NAME.

执行相应名称的模块(默认为command)

-m 后面是模块的名字

-a ‘ARGUMENTS’ –args=’ARGUMENTS’

The ARGUMENTS to pass to the module.

模块参数信息

-a后面是要执行的命令,也可以写一个ip,针对一台机器来执行命令

-C, –check Do not make any changes on the remote system, but test resources to see what might have changed.

不做任何改变,只是预测一些可能发生的改变(预演)

–syntax-check Look for syntax errors in the playbook, but don’t run anything.

在剧本上执行语法检查,但不执行剧本

温馨提示:本文最后更新于2022-12-20 20:57:41,已超过465天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/1922.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享