ansible自动化运维(二)——ansible基本部署及其配置文件的位置
文件目录
一.配置文件
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #主机清单
/etc/ansible/roles #存放角色的目录
1.主配置文件
Ansible的配置文件可以放在多个不同地方,优先级从高到低顺序如下
ansible_config #环境变量
./ansible.cfg #当前目录下的ansible.cfg
~/.ansible.cfg #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg #系统默认配置文件
Ansible的默认配置文件 /etc/ansible/ansible.cfg
[defaults]
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时命令执行目录
#forks = 5 # 默认并发数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#transport = smart
#remote_port = 22
#module_lang = C
#module_set_locale = False #检查对应的服务器的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
2.ansible需要知道三点:
执行导演:对谁做,主机清单文件决定的(主机清单文件叫什么,放在哪里都是由当前生效的ansible配置文件中的"inventory=" 参数决定的)
编剧:做什么,由playbook决定的
演员怎么做:ansible
2.1清单文件
主机清单文件(inventory、hosts)通常用于定义要管理主机的认证信息,例如ssh登录用户名、登录密码以及key相关信息。通过修改配置文件的方式来实现一系列操作。
在/etc/ansible/ansible.cfg中可通过修改 “inventory=” 参数来改变清单文件的位置
[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]# cat ansible.cfg
[defaults]
# some basic default values...
#inventory = /etc/ansible/hosts #默认路径
inventory = inventory #先添加的路径
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp
# 验证:
//添加受管主机
[root@localhost ansible]# vim inventory
192.168.58.30
[webservers]
192.168.58.40
//使用命令列出默认清单文件中所有受管主机
[root@localhost ansible]# ansible all --list-hosts
hosts (2):
192.168.58.30
192.168.58.40
二.如何使用帮助文档(模块)
1.查看三部曲
-
1.1查看所有模块列表
#ansible-doc -l
[root@localhost ansible]# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX/Thunder/vT...
a10_server_axapi3 Manage A10 Networks AX/S Manage A10 Networks AX/SoftAX/Thunder/vT...
a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vT...
aci_aaa_user
略------
Manage AAA users (aaa:User)
aci_aaa_user_certificate
-
1.2查看指定模块的帮助文档
ansible-doc 模块名称
[root@localhost ansible]# ansible-doc user
> USER (/usr/lib/python3.6/site-packages/ansible/modules/system/user.py)
Manage user accounts and user attributes. For Windows targets, use the
[win_user] module instead.
* This module is maintained by The Ansible Core Team
OPTIONS (= is mandatory):
- append
If `yes', add the user to the groups specified in `groups'.
If `no', user will only be added to the groups specified in `groups',
removing them from all other groups.
Mutually exclusive with `local'
[Default: False]
type: bool
- authorization
Sets the authorization of the user.
Does nothing when used with other platforms.
Can set multiple authorizations using comma separation.
To delete all authorizations, use `authorization='''.
Currently supported on Illumos/Solaris.
[Default: (null)]
type: str
version_added: 2.8
-
1.3查看模块可以使用的参数
ansible-doc -s 模块名称
[root@localhost ansible]# ansible-doc -s user
- name: Manage user accounts
user:
参数//append: # If `yes', add the user to the groups specified in `groups'. If `no', user
will only be added to the groups specified
in `groups', removing them from all other
groups. Mutually exclusive with `local'
参数//authorization: # Sets the authorization of the user. Does nothing when used with other
platforms. Can set multiple authorizations
using comma separation. To delete all
authorizations, use `authorization='''.
Currently supported on Illumos/Solaris.
comment: # Optionally sets the description (aka `GECOS') of user account.
create_home: # Unless set to `no', a home directory will be made for the user when the
account is created or if the home directory
does not exist. Changed from `createhome' to
`create_home' in Ansible 2.5.
参数//expires: 使用方法 # An expiry time for the user in epoch, it will be ignored on platforms that
do not support this. Currently supported on
GNU/Linux, FreeBSD, and DragonFlyBSD. Since
Ansible 2.6 you can remove the expiry time
specify a negative value. Currently
supported on GNU/Linux and FreeBSD.
参数//#force: 使用方法//# This only affects `state=absent', it forces removal of the user and
associated directories on supported
platforms. The behavior is the same as
`userdel --force', check the man page for
注意事项:
1.按q可退出帮助文档
2.左边为参数:右边为使用方法
三、Ansible 工具参数详解
Ansible 基于多模块管理,常用的 Ansible 工具管理模块包括:command、shell、script、yum、copy、File、async、docker、cron、mysql_user、ping、sysctl、user、acl、add_host、easy_install、haproxy 等。
Ansible 自动化批量管理工具主要参数如下:
参数 | 注释 |
---|---|
-v,–verbose | 打印详细模式; |
-i PATH,–inventory=PATH | 指定 host 文件路径; |
-f NUM,–forks=NUM | 指定 fork 开启同步进程的个数,默认 5; |
-m NAME,–module-name=NAME | 指定 module 名称,默认模块 command; |
-a MODULE_ARGS | module 模块的参数或者命令; |
-k,–ask-pass | 输入远程被管理端密码; |
–sudo | 基于 sudo 用户执行; |
-K,–ask-sudo-pass | 提示输入 sudo 密码与 sudo 一起使用; |
-u USERNAME,–user=USERNAME | 指定移动端的执行用户; |
-C,–check | 测试执行过程,不改变真实内容,相当于预演; |
-T TIMEOUT, | 执行命令超时时间,默认为 10 秒; |
–version | 查看 Ansible 软件版本信息。 |
1. 模块实战
1.1Ansible ping
Ansible 最基础的模块为 ping 模块,主要用于判断远程客户端是否在线,用于 ping本身服务器,返回值为 changed、ping。
Ansible ping 服务器状态
ansible -k all -m ping
[root@localhost ansible]# ansible all -m ping
192.168.58.30 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
1.2Ansible command
Ansible command 模块为 ansible 默认模块,主要用于执行 Linux 基础命令,可以执行远程服务器命令执行、任务执行等操作。
Command 模块使用详解:
参数 | 注释 |
---|---|
Chdir | 执行命令前,切换到目录; |
Creates | 当该文件存在时,则不执行该步骤; |
Executable | 换用 shell 环境执行命令; |
Free_form | 需要执行的脚本; |
Removes | 当该文件不存在时,则不执行该步骤; |
Warn | 如果在 ansible.cfg 中存在告警,如果设定了 False,不会警告此行。 |
Ansible command 模块企业常用案例如下:
(1) Ansible command 模块远程执行 date 命令,执行结果如图所示:
ansible -k -i /etc/ansible/inventory 192.168.58.30 -m command -a "date"
[root@localhost ansible]# ansible -k -i /etc/ansible/inventory 192.168.58.30 -m command -a "date"
SSH password:
192.168.58.30 | CHANGED | rc=0 >>
2021年 07月 15日 星期四 16:26:31 CST
[root@localhost ansible]#
(2) Ansible command 模块远程执行 ping 命令,执行结果如图所示:
[root@localhost ansible]# ansible -k 192.168.58.30 -m command -a "ping -c 1 www.baidu.com"
[root@localhost ansible]# ansible -k 192.168.58.30 -m command -a "ping -c 1 www.baidu.com"
SSH password:
192.168.58.30 | CHANGED | rc=0 >>
PING www.a.shifen.com (182.61.200.7) 56(84) bytes of data.
64 bytes from localhost (182.61.200.7): icmp_seq=1 ttl=128 time=27.8 ms
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 27.834/27.834/27.834/0.000 ms
[root@localhost ansible]#
(3) Ansible Hosts 正则模式远程执行 df -h,执行结果如图所示:
# Ansible command df -h 命令执行结果
ansible -k 192.168.149.13* -m command -a "df -h"
[root@localhost ansible]# ansible -k 192.168.58.30 -m command -a "df -h"
SSH password:
192.168.58.30 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 17M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-root 46G 2.3G 43G 5% /
/dev/nvme0n1p1 1014M 181M 834M 18% /boot
tmpfs 376M 0 376M 0% /run/user/0
1.3Ansible user
Ansible user 模块主要用于操作系统用户、组、权限、密码等操作,user 模块使用详解
参数 | 注释 |
---|---|
system | 默认创建为普通用户,为yes则创建系统用户; |
append | 添加一个新的组; |
comment | 新增描述信息; |
createhome | 给用户创建家目录; |
force | 用于删除强制删除用户; |
group | 创建用户主组; |
groups | 将用户加入组或者附属组添加; |
home | 指定用户的家目录; |
name | 表示状态,是否 create、remove、modify; |
password | 指定用户的密码,此处为加密密码; |
uid | 设置用户 id; |
state | 用户状态,默认为 present 表示新建用户。 |
Ansible user 模块企业常用案例如下:
(1) Ansible user 模块操作,name 表示用户名称,uid ,state表示其状态执行结果如图所示:
ansible -k 192.168.58.30 -m user -a "name=xx uid=2434 stste=present"
[root@localhost ansible]# ansible 192.168.58.30 -m user -a 'name=xx uid=2434 state=present'
192.168.58.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2434,
"home": "/home/xx",
"name": "xx",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2434
}
随后,在192.168.58.30主机上查看
root@localhost ~]# id xx
uid=2434(xx) gid=2434(xx) 组=2434(xx)
(2) Ansible user 模块操作,name 表示用户名称,state=absent 表示删除用户,执行结果如图所示:
ansible 192.168.58.30 -m user -a 'name=xx uid=2434 state=absent'
192.168.58.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "xx",
"remove": false,
"state": "absent"
}
随后,在192.168.58.30主机上查看
[root@localhost ~]# id xx
id: “xx”:无此用户