部署Ansible
1. Ansible模块类型
- 核心模块
- 附加模块
- 用户自定义模块
2.构建Ansible清单
2.1 定义清单
定义清单,将需要管理的一些主机分配到组中擎,以便进行集中管理(清单可以设置应用到它所定义的主机和组的变量)
有两种方式定义主机清单:
- 静态主机清单可以通过文本文件定义(可以使用多种不同的格式编写此文件,包括INI样式或YAML);
- 动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成
2.2 使用静态清单指定受管主机
方式一:可以使用多种不同的格式编写此文件,包括INI样式或YAML
alpha.example.org
beta.example.org
192.168.1.100
方式二:可以将受管主机组织为主机组。通过主机组,可以更加有效的对一系列系统运行Ansible
[webservers] #开头以中括号括起来为主机组名称,
alpha.example.org #跟在主机组后的受其主机管理
beta.example.org
192.168.1.100
www[001:006].example.com # " :"此符号表示" 到 ",如:www001.examlpe.com,www002.examlpe.com
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
db-[99:101]-node.example.com
注意:每行只能出现一个主机名,即便主机与主机之间出现空格也属于其主机,直到下一个主机组出现
- 例
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# ls
ansible.cfg hosts roles
[root@localhost ansible]# vim ansible.cfg
[root@localhost ansible]# touch inventory
[root@localhost ansible]# ls
ansible.cfg hosts inventory roles
[root@localhost ansible]# vim inventory
[root@localhost ansible]# cat inventory
192.168.142.192
[webservers]
192.168.142.192
node[01:15].examlpe.com
2.3 验证清单
[root@localhost ansible]# ansible 192.168.142.192 --list-hosts
hosts (1):
192.168.142.192
[root@localhost ansible]# ansible 192.168.142.191 --list-hosts
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.142.191
[WARNING]: No hosts matched, nothing to do
hosts (0):
运行以下命令来列出指定组中的所有主机:
[root@localhost ansible]# ansible webservers --list-hosts
hosts (16):
192.168.142.192
node01.examlpe.com
node02.examlpe.com
node03.examlpe.com
node04.examlpe.com
node05.examlpe.com
node06.examlpe.com
node07.examlpe.com
node08.examlpe.com
node09.examlpe.com
node10.examlpe.com
node11.examlpe.com
node12.examlpe.com
node13.examlpe.com
node14.examlpe.com
node15.examlpe.com
注意:如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。
2.4 覆盖清单的位置
/etc/ansible/hosts文件为系统的默认静态清单文件(通常不使用该文件,而是重新定义个不同位置的清单)
2.5 Ansible清单命令
修改默认清单文件/etc/ansible/hosts添加以下内容(例子如下):
192.168.142.192
[webservers]
192.168.142.191
192.168.142.190
列出默认清单文件中的所有受管主机:
ansible all --list-hosts
列出不属于任何组的受管主机:
ansible ungrouped --list-hosts
列出属于某组的受管主机:
ansible webservers --list-hosts
2.6 自定义清单文件
知道该文件位置列出所有受管主机:
ansible all -i /etc/ansible/inventory --list-hosts # " -i " 为指定文件位置
列出webservers组中的所有受管主机:
ansible webservers -i /etc/ansible/inventory --list-hosts
3. 管理Ansible配置文件
Ansible配置文件 | 说明 |
---|---|
/etc/ansible/ansible.cfg | 主配置文件 |
~/.ansible.cfg | 在当前目录中未找到ansible.cfg文件则使用该文件 |
./ansible.cfg | 执行ansible命令的目录中存在ansible.cfg文件 |
ANSIBLE_CONFIG环境变量 | 指定环境变量 |
inventory | 指定清单文件的路径 |
remote_user | 要在受管主机上登录的用户名。如果未指定则使用当前用户名 |
ask_pass | 是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false |
become | 连接后是否自动在受管主机上切换用户(通常切换为root)这也可以通过play来指定。 |
become_method | 如何切换用户(通常为sudo,这也是默认设置,但可选择su) |
become_user | 要在受管主机上切换到的用户(通常是root,这也是默认值) |
become_ask_pass | 是否需要为become_method提示输入密码。默认为false。 |
注意:在实践中不常使用~/.ansible.cfg或/etc/ansible/ansible.cfg文件,环境变量除非是要求必须在哪个环境中做才使用ANSIBLE_CONFIG环境变量
4. 配置文件优先级
-
首先找执行ansible命令的当前目录中,是否有 ansible.cfg文件
./ansible.cfg
-
如果找不到,再 找 当前用户的家目录下是否有 .ansible.cfg
~/.ansible.cfg
-
如果还找不到,就找 /etc/ansible/ansible.cfg
/etc/ansible/ansible.cfg
-
要检查所安装的Ansible版本以及正在使用的配置文件命令:
ansible --version
5. 查看帮助文档
[root@localhost ~]$ ansible [主机或组] -m [模块名] -a ['模块参数'] [ansible参数]
[root@localhost ~]$ ansible-doc -l # 列出所有核心模块和附加模块
[root@localhost ~]$ ansible-doc modulename #查询某个指定模块的文档使用方法(后加 -s 为简化)
官方文档:https://docs.ansible.com/ansible-core/2.11/collections/index_module.html
6.Ansible常用模块
6.1 文件模板
- copy:将本地文件复制到受管主机
- file:设置文件的权限和其他属性
- lineinfile:确保特定行是否在文件中
- synchronize:使用rsync同步内容
6.2 软件包模块
package:
- yum:使用yum管理软件包
- apt:使用APT管理软件包
- dnf:使用dnf管理软件包
- gem:管理Ruby gem
- pip:从PyPI管理Python软件包
6.3 系统模块
-
firewalld:使用firewalld管理防火墙
-
reboot:重启计算机
-
service:管理服务
-
user:添加、删除和管理用户帐户
Net Tools模块
- get_url:通过HTTP、HTTPS或FTP下载文件
- nmcli:管理网络
- uri:与Web服务交互## 6.基本模块的使用
例:
- ping模块使用:
[root@localhost ~]# ansible all -m ping
192.168.142.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
command模块使用:
指令模块、也是默认模块、不支持管道、通配符、脚本
[root@localhost ~]# ansible web -m command -a 'ls /root'
192.168.142.192 | CHANGED | rc=0 >>
anaconda-ks.cfg
Docker
initial-setup-ks.cfg
ll
公共
模板
视频
图片
文档
下载
音乐
桌面
[root@localhost ~]# ansible web -a 'ls /root'
192.168.142.192 | CHANGED | rc=0 >>
anaconda-ks.cfg
Docker
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
file模块使用:
创建目录,state值为:directory
[root@localhost ~]# ansible web -m file -a 'name=ly state=directory'
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "ly",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@localhost ~]# ansible web -m shell -a 'ls /root' #查看是否创建成功
192.168.142.192 | CHANGED | rc=0 >>
anaconda-ks.cfg
Docker
initial-setup-ks.cfg
ly
公共
模板
视频
图片
文档
下载
音乐
桌面
user模块使用:
管理用户(注意:密码可以使用加密)
[root@localhost ~]# ansible web -m user -a 'name=test1 password=1'
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly. #提醒:密码必须加密后此模板才可以正常运行
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"home": "/home/test********",
"name": "test********",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
}
yum软件包管理模块使用:
状态默认为present、也就是install
[root@localhost ~]# ansible web -m yum -a 'name=lly state=absent'
192.168.142.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"lly is not installed"
]
}