Ansible 是一个简单,强大且无代理的自动化语言。node
Ansible 的好处:python
简单易读:基于 YAML 文本编写,易于阅读,非专业的开发人员也能够编写。linux
功能强大:它能够同于管理配置,软件安装,流程自动化web
无代理:不须要在客户端安装额外的 agentshell
跨平台支持:支持 linux,Windows,Unix 和网络设备vim
Ansible 典型的工做方式是经过一个脚本文件(基于 YAML 格式构建的)去控制远端操做系统按照特定的顺序执行相关任务,咱们称这个文件为 playbook;bash
节点:Ansible 架构中拥有两种计算机类型,即控制节点和受控节点。Ansible 运行在控制节点上,而且只能运行在 linux 操做系统上,对于被控节点,能够是主机设备,也能够是网络设备,主机设备的操做系统,能够是 Windows,也能够是 linux。网络
清单(inventory): 受控节点设备的列表。在这个列表中,你能够根据某些标准(如,做用,服务等)将拥有相同属性的计算机组织到一个组中。Ansible 清单,支持静态清单(一旦定义好,除非你修改配置文件,否则不会发生改变。),也支持动态清单(经过脚本从外部源获取清单,该清单能够随着环境的改变而改变。)。架构
Playbook: 须要在被控节点主机上运行的任务列表,从而让他们达到咱们预期的状态。Playbook 中包含一个或多个 play,每一个 play 会在一组或多组计算机上按顺序执行已经定义好的一系列 task,每一个 task 都是一个执行模块。dom
模块(Module): 用于确保主机处于某一个特定的状态,例如能够使用 yum(对于不一样发行版本的 Linux,模块名可能有所不一样个,如,在 Ubuntu 中与之对应的是 apt 模块。) 模块确保主机已经安装了某个软件,若是主机状态已是预期的了(已经安装了该软件),那么就不会执行任何操做,执行下一个模块(task)。
Plugin 添加到 Ansible 中的代码段,用于扩展 Ansible 平台
在 Ubuntu 上安装 ansible
it@workstation:~$ sudo apt install -y ansible
安装完成后,你能够经过 ansible --version 查看 ansible 的版本信息
it@workstation:~$ ansible --version ansible 2.9.6 config file = /etc/ansible/ansible.cfg configured module search path = ['/home/it/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
Ansible 提供的默认主机配置文件:
it@workstation:~$ ls -l /etc/ansible/ansible.cfg -rw-r--r-- 1 root root 19985 3月 5 2020 /etc/ansible/ansible.cfg
* 只有 root 用户才有权限编辑。
建立新的主机配置文件:
通常状况,咱们直接复制默认配置文件,而后根据须要修改复制过来的配置文件。
it@workstation:~$ mkdir ansible it@workstation:~$ cp /etc/ansible/ansible.cfg ansible/ it@workstation:~$ ls -l ansible/ total 24 -rw-r--r-- 1 it it 19985 12月 29 15:03 ansible.cfg
* 在建立新的配置文件以前,咱们首先须要建立一个用于测试 Ansible 的工做目录,而后再建立配置文件。
当咱们拥有多个配置文件时,配置文件生效的顺序为:
当前目录下:./ansible.cfg
home 目录下:~/ansible.cfg
复制完成后,咱们能够经过 ansible --version 查看当前生效的配置文件
it@workstation:~$ cd ansible/ it@workstation:~/ansible$ ansible --version ansible 2.9.6 config file = /home/it/ansible/ansible.cfg configured module search path = ['/home/it/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
* 你须要切换到 Ansible 的工做目录,否则 Ansible 工做目录下的配置文件是无效的。
对于默认配置文件,咱们当前须要了解的有两个模块:defaults 和 privilege_escalation。
defaults 模块:
inventory: 指定清单文件路径
host_key_checking : 是否检查主机 key 是否可信
remote_user: 远程链接时使用的用户,默认使用当前用户
ask_pass: 链接时是否询问输入密码(若是没有配置密钥登陆,须要配置该选项为 true。)
privilege_escalation 模块:sudo 提权相关的配置
become: 是否开启切换用户
become_method: 如何切换用户
become_user: 切换到那个用户
become_ask_pass: 是否提示输入密码
更改配置文件
it@workstation:~/ansible$ vim ansible.cfg it@workstation:~/ansible$ grep -vE '^$|#' ansible.cfg [defaults] inventory = /home/it/ansible/hosts host_key_checking = False remote_user = it ask_pass = True [inventory] [privilege_escalation] become=True become_method=sudo become_user=root become_ask_pass=True [paramiko_connection] [ssh_connection] [persistent_connection] [accelerate] [selinux] [colors] [diff]
Ansible 提供了一个示例清单文件,并给咱们提供了一些常规的示例:
EX 1: 将未分组的主机放在全部组的前面的;
EX 2: 经过 [ ] 指定主机组的名称,如,webservers,下面直到下一个组名(dbservers)前结束的都是属于该组的主机,即便主机与主机之间存在空行,但如没有到下一个组名,他们依然属于同一个主机组;若是某些主机之间有某些顺序关系,你能够经过简写,将他们放到同一行,如示例中的 “www[001:006].example.com”,分别表示 www001.example.com、www002.example.com、www003.example.com、www004.example.com、www005.example.com 和 www006.example.com。
EX 3: 提供了另外一种主机范围的示例
it@workstation:~$ cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, specify before any group headers. #green.example.com #blue.example.com #192.168.100.1 #192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group #[webservers] #alpha.example.org #beta.example.org #192.168.1.100 #192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: #www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group #[dbservers] # #db01.intranet.mydomain.net #db02.intranet.mydomain.net #10.25.1.56 #10.25.1.57 # Here's another example of host ranges, this time there are no # leading 0s: #db-[99:101]-node.example.com it@workstation:~$
建立本身的主机清单
it@workstation:~$ vim ansible/hosts it@workstation:~$ cat ansible/hosts serverb [web] servera [prod:children] web
在该清单中,咱们使用组嵌套,这个是前面示例中没有的,web 组是 prod 组的子组。
咱们能够经过 ansible 命令查看主机清单内容:
it@workstation:~/ansible$ ansible web --list-host SSH password: BECOME password[defaults to SSH password]: hosts (1): servera it@workstation:~/ansible$ ansible prod --list-host SSH password: BECOME password[defaults to SSH password]: hosts (1): servera
咱们能够经过 ansible 命令来验证咱们的主机清单文件
同时 Ansible 还有两个默认组:
all: 表示全部组件
ungrouped: 表示全部未分组的主机
it@workstation:~/ansible$ ansible all --list-host SSH password: BECOME password[defaults to SSH password]: hosts (2): serverb servera it@workstation:~/ansible$ ansible ungrouped --list-host SSH password: BECOME password[defaults to SSH password]: hosts (1): serverb
* 在 Ansible 中,主机能够同时属于多个组。
Ansible 中存在一个隐藏的主机 localhost,即 ansible 自己,当主机指定为 localhost 时,ansible 会自动忽略掉 remote_user 配置;
Ansible 临时命令能够快速执行单个 ansible 任务,而不需编写 playbook,这对测试和排错颇有帮助。如,你能够使用临时命令去检查,某个软件包在主机上的状态是什么,是否可用。
经过临时命令查看链接到远程主机的用户信息
it@workstation:~/ansible$ ansible servera -m shell -a "id" SSH password: BECOME password[defaults to SSH password]: servera | CHANGED | rc=0 >> uid=0(root) gid=0(root) groups=0(root)
* 因为咱们没有配置免密(密钥),因此这里须要咱们输入输入两次密码,一次时 ssh 链接的密码,一次是 sudo 提权的密码;
* 若是使用 ssh 密码方式运行 ansible,你还须要安装 sshpass,否则会有报错;
it@workstation:~/ansible$ ansible servera -m shell -a "id" SSH password: BECOME password[defaults to SSH password]: servera | FAILED | rc=-1 >> to use the 'ssh' connection type with passwords, you must install the sshpass program
安装 sshpass
it@workstation:~/ansible$ sudo apt install sshpass