部署Ansible

139 阅读4分钟

部署Ansible

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1. Ansible清单

1.1 清单的作用

作用:定义了ansible服务器管理的一批受管主机,通过执行Ansible模块,在调用过程中直接对清单中的主机进行批量管理

1.2清单文件的位置

清单配置文件的默认位置在 /etc/ansible/hosts,可以通过修改该配置文件来指定不同的默认清单文件。可以使用不同的格式编写此文件,包括INI样式和YAML。

对于默认清单我们一般不使用,通常为了方便每一位管理者的管理,管理者均会在自己的文件夹目录中设置自己的清单目录。

1.3 清单文件的优先级

Ansible配置是以ini格式来存储数据的,在Ansible中,几乎所有配置都可以通过Ansible的Playbook或者环境变量来重新赋值。在运行Ansible命令时,命令将会按照预先设定的顺序查找配置文件。

  1. 首先使用 ANSIBLE_CONFIG 变量定义的文件位置
  2. 如果没有使用参数去定义文件位置,就会在执行 ansbile 命令的当前目录中查找是否有 ansible.cfg 文件 ./anisble.cfg
  3. 如果没找到,再从当前用户的家目录下查找是否有 .ansible.cfg 文件 ~/.ansible.cfg
  4. 如果还找不到,就使用默认清单 /etc/ansible/anisble.cfg /etc/ansible/ansible.cfg

简单来说,优先级为:

ANSIBLE_CONFIG > 当前目录下的ansible.cfg > ~/.ansible.cfg > /etc/ansible/ansible.cfg

要检查当前使用的配置文件的位置,使用 ansible --version 命令

[root@node1 ~]# ansible --version
ansible 2.9.23
  config file = /etc/ansible/ansible.cfg		//在这里可以看到我使用的是默认配置文件
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  3 2020, 18:11:24) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
[root@node1 ~]# 

2. Ansible配置文件

2.1 ansible配置文件详解

Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分标题以中括号括起来。对于基本操作,请使用以下两部分:

2.1.1 [defaults]修改默认设置

  • [defaults] 部分设置Ansible操作的默认值

例如:

[defaults]								
inventory = /etc/ansible/inventory			
remote_user = user					
ask_pass = false					     					

配置项说明:

  • inventory: 定义默认使用的主机清单
  • remote_user: ansible在操作远程主机时,使用远程主机上的哪个用户身份,默认是root
  • ask_pass: 执行ansible命令是否询问ssh密码

2.1.2 [privilege_escalation]提权

  • [privilege_escalation] 配置Ansible如何在受管主机上执行特权升级

例如:

[privilege_escalation]
become =Ture			
become_method = sudo	
become_user = root		 
become_ask_pass = False	  

配置项说明:

  • become: 是否允许远程主机上的普通用户进行提权
  • become_method: 如果允许提权,使用何种提权方式,默认是sudo
  • become_user: 设置提权到哪个用户,默认是root
  • become_ask_pass: 提权时,是否提示密码验证,默认时flase

2.2 配置文件注释

Ansible配置文件允许使用两种注释字符:井号或分号

位于行开头的#会注释掉整行。它不能和命令位于同一行中,如:

//正确
#become_method=sudo

//错误
become_method=#sudo

针对上面那种情况,我们可以使用分号来进行注释。分号字符可以注释掉所在行其右侧的所有内容。它可以和指令位于同一行中,只要该指令在其左侧,如:

become_method=;sudo				//这里注释掉了sudo

3. Ansible查看帮助文档

这里我们使用ansible-doc获取command模块的使用方式。 记住这个命令就没有不会用的模块,Ansible是一个非常容易上手的工具。

[root@node1 ~]# ansible-doc command
> COMMAND    (/usr/lib/python3.6/site-packages/ansible/modules/commands/command.py)

        The `command' module takes the command name followed by a list
        of space-delimited arguments. The given command will be
        executed on all selected nodes. The command(s) will not be
        processed through the shell, so variables like `$HOME' and
        operations like `"<"', `">"', `"|"', `";"' and `"&"' will not
        work. Use the [shell] module if you need these features. To
        create `command' tasks that are easier to read than the ones
        ......

因为信息太多,需要按下 “:” 之后再按下 “/” 之后再输入 <要查看部分的关键字>

最常用的要查看部分的关键字是:example

  • 列出所有的核心模块和附加模块
[root@node1 ~]# ansible-doc -l
a10_server                                                    Manage A10 Networ...
a10_server_axapi3                                             Manage A10 Networ...
a10_service_group                                             Manage A10 Networ...
a10_virtual_server                                            Manage A10 Networ...
aci_aaa_user                                                  Manage AAA users ...
aci_aaa_user_certificate                                      Manage AAA user c...
aci_access_port_block_to_access_port                          Manage port block...
aci_access_port_to_interface_policy_leaf_profile              Manage Fabric int...
aci_access_sub_port_block_to_access_port                      Manage sub port b...
aci_aep                                                       Manage attachable...
aci_aep_to_domain                                             Bind AEPs to Phys
......
  • 查询某个模块的使用方法,比较简洁的信息
ansible-doc -s modulename

如:
[root@node1 ~]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success
  ping:
      data:                  # Data to return for the `ping' return value. If this
                               parameter is set to
                               `crash', the module
                               will cause an
                               exception.
[root@node1 ~]# 

Ansible官方文档:docs.ansible.com/ansible/lat…

4. Ansible常用模块

Ansible模块分三种类型:

核心模块(code module)

附加模块(extra module)

用户自定义模块(consume module)

4.1 user模块

user模块主要用于远程批量创建用户信息

官方文档:docs.ansible.com/ansible/lat…

参数选项/默认值释义
password指定密码
name指定用户名
uid指定用户uid
group指定用户主要属于哪个组
groups指定用户属于哪个附加组信息
shell/bin/bash 或 /sbin/nologin指定是否能够登录
create_homeyes/no是否创建家目录信息
home指定家目录创建在什么路径 默认/home

password设置密码时不能使用明文方式,只能使用密文方式

可以给用户设置密码,还可以给用户修改密码

[root@node1 ~]# python3			//这里使用python环境先给密码加密
Python 3.6.8 (default, Dec  3 2020, 18:11:24) 
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt
>>> crypt.crypt('123456')
'$6$VhH3wY5ue6C.40aL$OzBiBs19nMbQZCnNnKXY5cPemxJSPlt2C1JP.XxAyegC.3Lyn1XnneHE2rJ7iGkaKXBe.8TBKo8KHP4wUSAsH/'			//得到加密后的密码
>>> exit()
[root@node1 ~]# ansible all -m user -a 'name=test uid=1234 password="$6$VhH3wY5ue6C.40aL$OzBiBs19nMbQZCnNnKXY5cPemxJSPlt2C1JP.XxAyegC.3Lyn1XnneHE2rJ7iGkaKXBe.8TBKo8KHP4wUSAsH/"'			//这里输入加密后的密码		
192.168.100.110 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1234,
    "home": "/home/test",
    "name": "test",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1234
}
[root@node1 ~]# 

//在node2中查看是否创建成功
[root@node2 ~]# id test
uid=1234(test) gid=1234(test) groups=1234(test)