Linux-Ansible-安装

ansible的安装方法有多种

官方文档

https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.ht

下载

https://releases.ansible.com/ansible/

pip 下载

https://pypi.org/project/ansible/

yum安装

[root@centos7-liyj /etc/yum.repos.d]#yum install  ansible
Dependencies Resolved

=======================================================================================================================
 Package                                        Arch              Version                      Repository         Size
=======================================================================================================================
Installing:
 ansible                                        noarch            2.9.27-1.el7                 epel               17 M
Installing for dependencies:
 PyYAML                                         x86_64            3.10-11.el7                  BASE              153 k
 libyaml                                        x86_64            0.1.4-11.el7_0               BASE               55 k
 python-babel                                   noarch            0.9.6-8.el7                  BASE              1.4 M
 python-backports                               x86_64            1.0-8.el7                    BASE              5.8 k
 python-backports-ssl_match_hostname            noarch            3.5.0.1-1.el7                BASE               13 k
 python-cffi                                    x86_64            1.6.0-5.el7                  BASE              218 k
 python-enum34                                  noarch            1.0.4-1.el7                  BASE               52 k
 python-idna                                    noarch            2.4-1.el7                    BASE               94 k
 python-ipaddress                               noarch            1.0.16-2.el7                 BASE               34 k
 python-jinja2                                  noarch            2.7.2-4.el7                  BASE              519 k
 python-markupsafe                              x86_64            0.11-10.el7                  BASE               25 k
 python-paramiko                                noarch            2.1.1-9.el7                  BASE              269 k
 python-ply                                     noarch            3.4-11.el7                   BASE              123 k
 python-pycparser                               noarch            2.14-1.el7                   BASE              104 k
 python-setuptools                              noarch            0.9.8-7.el7                  BASE              397 k
 python-six                                     noarch            1.9.0-2.el7                  BASE               29 k
 python2-cryptography                           x86_64            1.7.2-2.el7                  BASE              502 k
 python2-httplib2                               noarch            0.18.1-3.el7                 epel              125 k
 python2-jmespath                               noarch            0.9.4-2.el7                  epel               41 k
 python2-pyasn1                                 noarch            0.1.9-7.el7                  BASE              100 k
 sshpass                                        x86_64            1.06-2.el7                   extras             21 k

Transaction Summary
=======================================================================================================================
Install  1 Package (+21 Dependent packages)

Total download size: 21 M
Installed size: 122 M

编译安装

yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
wget https://releases.ansible.com/ansible/ansible-1.5.4.tar.gz
tar xf ansible-1.5.4.tar.gz
cd ansible-1.5.4
python setup.py build
python setup.py install
mkdir /etc/ansible
cp -r examples/* /etc/ansible

pip 安装

pip 是安装Python包的管理器,类似 yum

[root@centos7 ~]#yum install python-pip
[root@centos7 ~]#pip install --upgrade pip
[root@centos7 ~]#pip install ansible --upgrade
[root@centos7 ~]#ansible --version
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39:
CryptographyDeprecationWarning: Python 2 is no longer supported by the Python
core team. Support for it is now deprecated in cryptography, and will be removed
in a future release.
 CryptographyDeprecationWarning,
ansible 2.9.12
 config file = None
 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, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623
(Red Hat 4.8.5-39)]
[root@centos7 ~]#ll /opt/etc/ansible/ansible.cfg
-rw-r--r-- 1 wang bin 19980 Aug 11 21:34 /opt/etc/ansible/ansible.cfg

 ansible 相关文件

配置文件

/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性,也可以在项目的目录中创建此文件,当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文件

/etc/ansibel/hosts  主机清单

/etc/ansibel/roles/ 存放角色的目录

ansible主配置文件

ansible的配置文件可以存放在多个不同地方,优先级从高到底顺序如下

ANSIBLE_CONFIG        #环境变量,注意此项用 ansible --version 看不到,但可以生效
./ansible.cfg         #当先目录下的ansible.cfg
~/.ansible.cfg        #当前用户家目录下的 .ansible.cfg
/etc/ansible/ansible.cfg #系统默认配置文件

ansible的默认配置文件 /etc/ansible/ansible.cfg,其中大部分的配置内容无需进行修改

[defaults]
#inventory = /etc/ensible/hosts #主机列表配置文件
#libray = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port =22
#host_key_checking = Fales #检查对应服务器的host_key,建议取消此行注释,实现第一次连接自动信任目标主机
#log_path=/var/log/ansible.log #日志文件,建议启用
#module_name = command #默认模块,可以修改为shell模块

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

 inventory 主机清单文件

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory 主机清单文 件中将其分组组织

默认的inventory file为 /etc/ansible/hosts inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成

注意: 生产建议在每个项目目录下创建项目独立的hosts文件

主机清单文件格式

inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中

此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明 如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机

Inventory 参数说明

 1 ansible_ssh_host #将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
 2 ansible_ssh_port #ssh端口号.如果不是默认的端口号,通过此变量设置.这种可以使用 ip:端口
 3 192.168.1.100:2222
 4 ansible_ssh_user #默认的 ssh 用户名
 5 ansible_ssh_pass #ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
 6 ansible_sudo_pass #sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
 7 ansible_sudo_exe (new in version 1.8) #sudo 命令路径(适用于1.8及以上版本)
 8 ansible_connection #与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默
 9 认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
10 ansible_ssh_private_key_file #ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
11 ansible_shell_type #目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为'csh''fish'.
12 ansible_python_interpreter #目标主机的 python 路径.适用于的情况: 系统中有多个 Python,或者命令路径不是"/usr/bin/python",
比如 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的Python.之所以不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,
且要求 "python"可执行程序名不可为 python以外的名字(实际有可能名为python26).与ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

 

posted @ 2022-05-16 19:14  goodbay说拜拜  阅读(371)  评论(0编辑  收藏  举报