ansible的部署
一、构建ansible清单
1、定义清单
清单定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。
可以通过两种方式定义主机清单。静态主机清单可以通过文本文件定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成。
2、使用静态清单指定受管主机
静态清单文件是指定Ansible目标受管主机的文本文件。可以使用多种不同的格式编写此文件,包括INI样式或YAML。
2.1 清单文件的写法:
//先进入/etc/ansible/
[root@wjj ~]# cd /etc/ansible/
您在 /var/spool/mail/root 中有新邮件
[root@wjj ansible]# ls
ansible.cfg hosts roles //hosts是默认的清单文件
//进入清单配置文件,把hosts修改为inventory
[root@wjj ansible]# vim ansible.cfg
#inventory = /etc/ansible/hosts
inventory = /etc/ansible/inventory
[root@wjj ansible]# ls
ansible.cfg hosts roles
//创建inventory文件,在进入里面写东西
[root@wjj ansible]# touch inventory
[root@wjj ansible]# vim inventory
192.168.47.222
[webservers]
192.168.47.222 //192.168.47.222既是单独的也是一个组里面的成员
//用组的名字去查找并列出组里面(所有)IP地址
[root@wjj ansible]# ansible webservers --list-hosts
hosts (1):
192.168.47.222
//用ip地址去查找并列出
[root@wjj ansible]# ansible 192.168.47.222 --list-hosts
hosts (1):
192.168.47.222
3、验证清单
[root@wjj ansible]# vim inventory
192.168.47.222
[webservers]
192.168.47.222
1.1.1.1
node[1:10].example.com
//若有疑问,可使用 ansible 命令验证计算机是否存在于清单中:
[root@wjj ansible]# ansible webservers --list-hosts
hosts (12):
192.168.47.222
1.1.1.1
node1.example.com
node2.example.com
node3.example.com
node4.example.com
node5.example.com
node6.example.com
node7.example.com
node8.example.com
node9.example.com
node10.example.com
如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。
应对这种情况的方法有多种,其中最简单的是确保主机组不使用与清单中主机相同的名称。
4、构建Ansible清单
使用以下命令列出默认清单文件中的所有受管主机(不包含主机):
[root@wjj ansible]# ansible all --list-hosts
使用以下命令列出不属于任何组的受管主机:
[root@wjj ansible]# ansible ungrouped --list-hosts
使用以下命令列出属于某组的受管主机:
[root@wjj ansible]# ansible webservers --list-hosts
二、管理ansible配置文件
1、配置文件位置
在[defaults]部分中,inventory指令可以直接指向某一静态清单文件,或者指向含有多个静态清单文件和动态清单脚本的某一目录。
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts // 定义inventory 可以是目录或文件名
inventory = /etc/ansible/inventory //刚修改的清单新位置
#library = /usr/share/my_modules/ // 自定义lib库存放目录
#module_utils = /usr/share/my_module_utils/ //模块菜单的位置
#remote_tmp = ~/.ansible/tmp //临时文件远程主机存放目录
#local_tmp = ~/.ansible/tmp //临时文件本地存放目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 **默认开启的并发数,对于大批量操作加大并发数可以提高效率
#poll_interval = 15 //默认轮询时间间隔
#sudo_user = root //默认sudo用户
#ask_sudo_pass = True // 是否需要sudo密码
#ask_pass = True //是否需要密码
#transport = smart //通信机制默认为samart
#remote_port = 22 //远程SSH端口号 22号
#module_lang = C //模块和系统之间通信的计算机语言,默认 C语言
#module_set_locale = False
2、配置文件优先级
从低到高:
使用/etc/ansible/ansible.cfg
ansible软件包提供一个基本的配置文件,它位于/etc/ansible/ansible.cfg。如果找不到其他配置文件,则使用此文件。
使用~/.ansible.cfg
Ansible在用户的家目录中查找.ansible.cfg文件。如果存在此配置文件并且当前工作目录中也没有ansible.cfg文件,则使用此配置取代/etc/ansible/ansible.cfg。
使用./ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。
推荐的做法是在需要运行Ansible命令的目录中创建ansible.cfg文件。此目录中也将包含任何供Ansible项目使用的文件,如清单和playbook。这是用于Ansible配置文件的最常用位置。实践中不常使用~/.ansible.cfg或/etc/ansible/ansible.cfg文件
使用ANSIBLE_CONFIG环境变量
我们可以通过将不同的配置文件放在不同的目录中,然后从适当的目录执行Ansible命令,以此利用配置文件。但是,随着配置文件数量的增加,这种方法存在局限性并且难以管理。有一个更加灵活的选项,即通过ANSIBLE_CONFIG环境变量定义配置文件的位置。定义了此变量时,Ansible将使用变量所指定的配置文件,而不用上面提到的任何配置文件。
ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该变量,则接下来检查运行ansible命令的目录中是否有ansible.cfg文件。如果不存在该文件,则检查用户的家目录是否有.ansible.cfg文件。只有在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件。如果/etc/ansible/ansible.cfg配置文件不存在,Ansible包含它使用的默认值。
3、配置文件中的指令
指令 | 描述 |
---|---|
inventory | 指定清单文件的路径 |
remote_user | 要在受管主机上登录的用户名。如果未指定则使用当前用户名 |
ask_pass | 是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false |
become | 连接后是否自动在受管主机上切换用户(通常切换为root)这也可以通过play来指定。 |
become_method | 如何切换用户(通常为sudo,这也是默认设置,但可选择su) |
become_user | 要在受管主机上切换到的用户(通常是root,这也是默认值) |
become_ask_pass | 是否需要为become_method提示输入密码。默认为false。 |
4、查看帮助文档
//当找不到帮助文档的时候,可以使用ansible-doc -l | grep [关键字] 命令查找
例:
[root@wjj ansible]# ansible-doc -l|grep user //查找跟用户有关的所有模块
//如果知道模块的名字,就用ansible-doc [模块的名字] 命令就可以查看到它的帮助文档
[root@wjj ansible]# ansible-doc user
5、Ansible常用模块
文件模块:
- copy:将本地文件复制到受管主机
- file:设置文件的权限和其他属性
- lineinfile:确保特定行是否在文件中
- synchronize:使用rsync同步内容
软件包模块:
- package:使用操作系统本机的自动检测软件包管理器管理软件包
- yum:使用yum管理软件包
- apt:使用APT管理软件包
- dnf:使用dnf管理软件包
- gem:管理Ruby gem
- pip:从PyPI管理Python软件包
系统模块:
- firewalld:使用firewalld管理防火墙
- reboot:重启计算机
- service:管理服务
- user:添加、删除和管理用户帐户
Net Tools模块:
- get_url:通过HTTP、HTTPS或FTP下载文件
- nmcli:管理网络
- uri:与Web服务交互
模块的使用
hostname模块:
[root@wjj ansible]# ansible localhost -m hostname -a"name=localhost"
localhost | CHANGED => {
"ansible_facts": {
"ansible_domain": "localdomain",
"ansible_fqdn": "localhost.localdomain",
"ansible_hostname": "localhost",
"ansible_nodename": "localhost"
},
"changed": true,
"name": "localhost"
}
[root@wjj ansible]# bash
[root@localhost ansible]#
user模块:
//创建用户
[root@wjj ansible]# ansible localhost -m user -a"name=wjj state=present"
localhost | CHANGED => {
"changed": true,
"comment": "",
"create_home": true,
"group": 1002,
"home": "/home/wjj",
"name": "wjj",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}
//修改用户uid
[root@wjj ansible]# ansible localhost -m user -a"name=wjj uid=1003"
[root@wjj ansible]# id wjj
uid=1003(wjj) gid=1003(wjj) 组=1003(wjj)
//删除用户
[root@wjj ansible]# ansible localhost -m user -a"name=wjj state=absent"
localhost | CHANGED => {
"changed": true,
"force": false,
"name": "wjj",
"remove": false,
"state": "absent"
}
[root@wjj ansible]# id wjj
id: wjj: no such user
group模块:
//创建组
[root@wjj ansible]# ansible localhost -m group -a"name=jing state=present"
localhost | CHANGED => {
"changed": true,
"gid": 1004,
"name": "jing",
"state": "present",
"system": false
}
//修改组gid
[root@wjj ansible]# ansible localhost -m group -a"name=jing gid=1005"
localhost | CHANGED => {
"changed": true,
"gid": 1005,
"name": "jing",
"state": "present",
"system": false
}
//删除组
[root@wjj ansible]# ansible localhost -m group -a"name=jing state=absent"
localhost | CHANGED => {
"changed": true,
"name": "jing",
"state": "absent"
}
mkdir模块:
[root@wjj ansible]# ansible localhost -m command -a"mkdir mulu"
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command
because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
localhost | CHANGED | rc=0 >>
[root@wjj ansible]# ls
ansible.cfg hosts inventory mulu roles
touch模块:
[root@wjj ansible]# ansible localhost -m command -a"touch wjj"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command
because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
localhost | CHANGED | rc=0 >>
[root@wjj ansible]# ls
ansible.cfg hosts inventory mulu roles wjj
copy模块:
[root@wjj ansible]# ansible localhost -m copy -a"src=/etc/ansible/123 dest=/home"
localhost | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/home/123",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_home_dir_t:s0",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1626380119.67-20234-35250722537954/source",
"state": "file",
"uid": 0
}