一、前期准备
1、安装
如果yum源没有ansible,需要提前配置yum源:
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
# 可以使用以下三种方式查看ansible包的信息
yum info ansible
yum list ansible
yum list | grep ansible
# 安装ansible
yum -y install ansible
ansible --version
2、配置免密登录
- 1、生成公钥
- 2、编写脚本分发公钥
public=(
172.16.17.10
172.16.17.11
172.16.16.61
)
for i in ${public[*]}
do
sshpass -p '登陆密码' ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no ${i}
done
二、常用模块
1、查看模块说明
#查看所有模块:
ansible-doc -l
#查看具体模块,比如查看 setup 模块:
ansible-doc setup
#查看 setup 模块的示例输出:
ansible-doc -s setup
2、ansible命令运行方式及常用参数
3.ansible的基本颜色代表
4、assert模块
assert 模块的语法:
- name: Ensure that a variable is set to a specific value
assert:
that:
- variable_name == expected_value
下面示例使用 assert 模块检查变量m_var 是否为字符串类型:
- name: Ensure that a variable is a string
assert:
that:
- m_var is string
如果 m_var 不是字符串类型,则任务将失败。
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
fail_msg | string | 用于失败断言的自定义消息 | |
success_msg | string | 用于成功断言的自定义消息 | |
that | list | 可以传递给when语句的相同形式的字符串表达式列表 | |
quiet | boolean | false | 将此设置为true以避免冗长的输出 |
5、authorized_key模块
以下是一些常见的用法:
添加公钥到远程主机的授权密钥列表中:
- name: Add SSH key to authorized_keys
authorized_key:
user: username
key: "{{ lookup('file', '/path/to/public/key') }}"
从远程主机的授权密钥列表中删除公钥:
- name: Remove SSH key from authorized_keys
authorized_key:
user: username
key: "{{ lookup('file', '/path/to/public/key') }}"
state: absent
从远程主机的授权密钥列表中删除所有公钥:
- name: Remove all SSH keys from authorized_keys
authorized_key:
user: username
state: absent
6、at模块
##at 模块示例:
- name: remove tempuser
at:
command: userdel -r tempuser
count: 20
units: minutes
unique: yes
参数 | 选项 | 注释 |
command | Null | 计划运行的命令 |
count | Null | 单位数。(必须和units一起运行) |
script_file | Null | 要在将来执行的现有脚本文件 |
state | absent、present | 添加或删除命令或脚本的状态 |
unique | yes、no | 如果作业已经在运行,则不会再次执行 |
units | minutes/hours/days/weeks | 时间名称 |
7、blockinfile模块
ansible blockinfile模块用于在文件中添加或修改一个块(block)的内容。它可以在文件中查找一个特定的标记(marker),然后在该标记之前或之后添加或修改一个块的内容。
该模块的常用参数包括:
例如,以下是一个使用blockinfile模块添加一个块的示例:
- name: Add a block to a file
blockinfile:
path: /etc/nginx/nginx.conf
block: |
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8000;
}
}
marker: "# BEGIN NGINX BLOCK"
state: present
这个任务会在/etc/nginx/nginx.conf文件中查找标记# BEGIN NGINX BLOCK,如果找到了该标记,则在该标记之前添加一个块。如果没有找到该标记,则在文件末尾添加该块。
8、command、shell、raw、script模块
-
command模块
ansible all -m command -a 'tail /etc/passwd' -i inventory
chdir ##执行命令前先进入到指定目录
ansible all -m command -a 'chdir=/data pwd'
creates ##如果文件存在将不运行
ansible all -m command -a 'creates=/data/file touch /data/file'
removes ##如果文件存在再将运行
ansible all -m command -a 'removes=/data/file rm -rf /data/file'
-
shell模块
-
raw模块
raw模块用于在远程主机上执行命令,其支持管道符与重定向
ansible all -m raw -a 'echo "hello world"> /tmp/test';
ansible 192.168.160.137 -m raw -a 'cat /tmp/test|grep -Eo hello'
-
script模块
ansible执行本地脚本到目标服务器执行,注意脚本需要+x 权限
ansible demo -m script -a demo.sh
- 不同点
command:
可以在受管主机上执行 shell 命令,但是不支持环境变量和操作符(例如 '|', '<', '>', '&')
shell 模块调用的/bin/sh指令执行。
raw 模块不需要受管主机上安装Python,直接使用远程shell运行命令,通常用于无法安装Python的系统(例如网络设备等)。
9、copy模块
举例:
ansible all -m copy -a 'content="this server is managed by ansible\n" dest=/etc/motd' --become
copy模块的基本语法如下:
- name: Copy file from local to remote
copy:
src: /path/to/local/file
dest: /path/to/remote/file
owner: user
group: group
mode: 0644
如果要复制目录,可以使用递归选项:
- name: Copy directory from local to remote
copy:
src: /path/to/local/directory/
dest: /path/to/remote/directory/
owner: user
group: group
mode: 0755
recurse: yes
ansible copy模块常用参数:
group 指定文件所有组
ansible test -m copy -a 'src=./test.sh dest=/mnt owner=westos group=westos'
mode 指定目的地文件权限
ansible test -m copy -a 'src=./test.sh dest=/mnt owner=westos group=westos mode=755'
backup=yes 当受控主机中存在文件时备份原文件
ansible test -m copy -a 'src=./test.sh dest=/mnt owner=westos group=westos mode=755 backup=yes'
content 指定文本内容直接在受控主机中生成文件
ansible test -m copy -a 'dest=/mnt/file owner=westos group=westos mode=755 content="hellolinux"'
10、cron模块
job :任务脚本或命令
disabled :yes 禁用计划任务 ,no 启动计划任务
state :absent 删除计划任务
ansible test -m cron -a 'job="echo test" name=lee minute=*/2'
##job:要做的动作
##name:cron的名字
## */2:每隔两分钟执行一次
ansible test -m cron -a 'job="echo test" name=lee disabled=yes'
disabled=yes:禁用
disabled=no:启用
ansible test -m cron -a 'job="echo test" name=lee disabled=no state=absent'
state:删除任务
ansible test -m cron -a 'job="echo test" name=lee minute=*/2 day=1.5 user=westos'
## user=westos 指定用户身份
- cron:
name: "Flush Bolt"
user: "root"
minute: 45
hour: 11
job: "php ./app/nut cache:clear"
cron 模块的一些常用参数有:
参数 | 选项 | 注释 |
slecial_time | reboot、yearly、annually、monthly | 一系列重复出现时间 |
state | absent、present | present创建;absent删除 |
cron_file | Null | 如果有大量的服务器要维护,那么最好预先写好crontab文件 |
backup | yes、no | 在编辑之前备份crontab文件 |
11、fetch模块
src 受控主机的源文件 dest 本机目录
ansible test -m fetch -a 'src=/mnt/test.sh dest=/tmp'
flat 复制文件时,不复制文件的路径,只复制文件本身
ansible test -m fetch -a 'src=/mnt/test.sh dest=/tmp flat=yes'
12、unarchive:解压缩
ansible all -m unarchive -a 'src=/data/aaa.bz2 dest=/data copy=no'
13、archive:压缩
ansible all -m archive -a 'path=/data dest=/data/test/test.tar.gz2 format=bz2 owner=test mode=755'
14、hostname
管理主机名称
ansible 192.168.0.1 -m hostname -a 'name=test.com'
15、yum_repository
配置系统软件仓库源文件
name | 指定仓库名称 |
baseurl | 指定源路径 |
description | 指定仓库描述 |
file | 指定仓库文件名称 |
enabled | 仓库是否启用 |
gpgcheck | 仓库是否检测gpgkey |
state | 默认值present建立 |
absent | 为删除 |
ansible all -m yum.repository -a 'file=/testos name=baseOS baseurl=http://192.168.0.1/xxx/baseOS description="test baseOS" gpgcheck=no enabled=yes'
16、dnf
管理系统中的dnf仓库及管理软件
name | 指定包 |
state | 指定动作 |
present安装 | |
latest更新 | |
absent删除 | |
list | 列出指定信息 |
httpd | |
installed | |
all | |
available | |
disable_gpg_check | 禁用gpgkey检测 |
enablerepo | 指定安装包来源 |
disablerepo | 禁用安装包来源 |
# 列出httpd
ansible test -m dnf -a 'list=httpd'
# 安装httpd
ansible test -m dnf -a 'name=httpd state=present'
# 卸载httpd
ansible test -m dnf -a 'name=httpd state=absent'
# 卸载的时候依赖也卸载
ansible test -m dnf -a 'name=httpd state=absent autoremove=yes'
17、service
管理系统服务状态
name | 指定服务名称 |
state | 指定对服务的动作 |
started | |
stopped | |
restarted | |
reloaded | |
enabled | 设定服务开机是否启动 |
yes开启启动 | |
no开机不启动 |
# 开启httpd
ansible test -m service -a 'name=httpd state=started enabled=yes'
# 查看是否开启
ansible test -m shell -a 'systemctl status httpd'
# 关闭httpd
ansible test -m service -a 'name=httpd state=stopped enabled=no'
18、user
# 创建 test 用户
ansible test -m user -a 'name=test'
# 删除 test 用户
ansible test -m user -a 'name=tesst state=absent'
# 删除 test 用户一起删除家目录
ansible test -m user -a 'name=tesst state=absent remove=yes'
# 指定 uid 创建用户
ansible test -m user -a 'name=test uid=1000 group=1000 groups=999'
19 、group
ansible test -m group -a 'name=test gid=999'
20、lineinfile
# 备份文件
tasks:
- name: 'backup file'
lineinfile:
path: /root/line.txt
backup: yes
line: state=present
# 替换行
tasks:
- name: 'replace'
lineinfile:
path: /root/line.txt
regexp: 'ddddeeeee'
line: '此行已被替换'
# 在匹配行前面插入
tasks:
- name: '在匹配行前面插入'
lineinfile:
path: /root/line.txt
insertbefore: 'aaaavvvaaaaaaaa'
line: '成功在匹配行前面插入一行'
# 在匹配行后面插入
tasks:
- name: "在匹配行后面插入"
lineinfile:
dest: /root/line.txt
insertafter: 'aassssaaaaaaaa'
line: 成功在匹配行后面插入一行
# 删除匹配行
tasks:
- name: '删除匹配行'
lineinfile:
dest: /root/line.txt
regexp: 's\+awc\_i\!t\@\#x'
state: absent
# 注意,需要删除的是 s+awc_i!t@#x 匹配需要转义特殊符号
# 新建文件
tasks:
- name: 'create'
lineinfile:
dest: /root/test.txt
create: yes
line: state=present