ansible模块
ansible基础
- 配置ansible的hosts文件
sudo vim /etc/ansible/hosts
添加以下内容
client1
client2
client3
- 测试ansible能否通
命令
ansible [主机名] -m ping -o
# -O 简洁输出
例:
ansible localhost -m ping -o
ansible client1 -m ping -o
可以看到结果如下
- ping自己可以通

- ping client1可以通

- ping client2和client3不可以通,因为没有配置免密,没有授权,无法识别到

如果要连接,可以在后面加上账号密码

- ping client4未匹配到,因为前面在hosts文件没有添加上

Inventory-主机清单
含义:清查;存货清单,财产目录,主机清单
- 增加主机组,增加后可以通过组进行管理主机
sudo vim /etc/ansible/hosts
添加以下内容
[webserver] #组名,可自定义
client1
client2
client3
client4
测试结果
ansible webserver -m ping -o -u ops -k --ssh-common-args="-o StrictHostKeyChecking=no"
#--ssh-common-args="-o StrictHostKeyChecking=no"作用是忽略ssh首次连接手动输入yes/no来进行公钥确认,也可以不写

- 增加用户名 密码
sudo vim /etc/ansible/hosts
添加以下内容
[webserver]
client1 ansible_ssh_user='ops' ansible_ssh_pass='password'
client2 ansible_ssh_user='ops' ansible_ssh_pass='password'
client3 ansible_ssh_user='ops' ansible_ssh_pass='password'
client4 ansible_ssh_user='ops' ansible_ssh_pass='password'
或
[webserver]
client[1:4] ansible_ssh_user='ops' ansible_ssh_pass='password'

3. 增加端口
修改client1的ssh端口
vim /etc/ssh/sshd_config
修改以下内容
#Port 22改为Port 2222
重启ssh服务
systemcetl restart sshd
到ansible服务器测试能否通,结果失败

添加端口
vim /etc/ansible/hosts
[webserver]
client1 ansible_ssh_user='ops' ansible_ssh_pass='password' ansible_ssh_port='222'
client2 ansible_ssh_user='ops' ansible_ssh_pass='password'
client3 ansible_ssh_user='ops' ansible_ssh_pass='password'
client4 ansible_ssh_user='ops' ansible_ssh_pass='password'
再次到ansible服务器测试能否通,结果成功

4. 组:变量
修改组变量
[webserver]
client1
client2
client3
client4
[webserver:vars]
ansible_ssh_user='ops'
ansible_ssh_pass='password'

常用组变量
| 参数 | 用途 | 例子 |
|---|---|---|
| ansible_ssh_host | 定义 hosts ssh 地址 | ansible_ssh_host=192.168.1.100 |
| ansible_ssh_port | 定义 hosts ssh 端口 | ansible_ssh_port=3000 |
| ansible_ssh_user | 定义 hosts ssh 认证用户 | ansible_ssh_user=user |
| ansible_ssh_pass | 定义 hosts ssh 认证密码 | ansible_ssh_pass=pass |
| ansible_sudo | 定义 hosts sudo 密码 | ansible_sudo=www |
| ansible_sudo_exe | 定义 hosts sudo 路径 | ansible_sudo_exe=/usr/bin/sudo |
| ansible_connection | 定义 hosts 连接方式 | ansible_connection=local |
| ansible_ssh_private_key_file | 定义 hosts 私钥 | ansible_ssh_private_key_file=/root/key |
| ansible_ssh_shell_type | 定义 hosts shell类型 | ansible_ssh_shell_type=bash |
| ansible_python_interpreter | 定义 hosts 任务执行python路径 | ansible_python_interpreter=/usr/bin/python2.6 |
| ansible_*_interpreter | 定义 hosts其他语言解析路径 | ansible_*_interpreter=/usr/bin/ruby |
- 子分组
将不同的组合并到一个组
[apacheserver]
client1
client2
[tomcatserver]
client3
client4
[webserver:children]
apacheserver
tomcatserver
测试



6. 自定义主机列表
把hosts文件移动到家目录下
sudo mv /etc/ansible/hosts ~

使用-i参数调用家目录下的hosts文件
ansible -i hosts webserver -m ping -o -u ops -k

做完实验后切记把hosts文件还原回去,以免影响下面的实验
Ad-Hoc-点对点模式
简介
- shell模块
bash指令让目标去执行
案例
(1)查看目标服务器的主机名
ansible webserver -m shell -a 'hostname' -k

(2)让其他服务器安装vsftp
ansible webserver -m shell -a 'yum install -y vsftpd' -k -b -K
验证

2. 复制模块
- 帮助
ansible-doc copy
- 案例
(1)将服务器的/etc/ansible/hosts文件分发到各个client
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' -u ops -k
到各个client查看
[ops@ansible-client1 ~]$ cat /tmp/2.txt

(2)将服务器的/etc/ansible/hosts文件分发到各个client并备份
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' -u ops -k backup=yes
#backup=yes表示当有同名的原文件存在时则进行备份
- 用户模块
- 创建用户
ansible webserver -m user -a 'name=xiaodunan state=present' -u ops -k --become-user root -K --become
#-k:ssh用户的密码;-K:sudo的密码;--become-user:用sudo的用户(默认是root,可不写);--become:提升权限

到client验证用户是否存在
id xiaodunan

- 修改密码
(1)生成加密密码
echo "123456" | openssl passwd -1 -stdin

(2)修改密码
ansible webserver -m user -a 'name=xiaodunan password="$1$aQkYQKlU$VSaWvQPHXVopR/KBrGbwA1"' -b -K -u ops -k
验证


- 修改shell
ansible webserver -m user -a 'name=xiaodunan shell=/sbin/nologin append=yes' -o -k -b -K

验证

- 删除用户
ansible webserver -m user -a 'name=xiaodunan state=absent' -o -k -b -K
验证

4. 软件包管理
- 对httpd进行更新
ansible webserver -m yum -a 'name="httpd" state=latest' -o -u ops -k -b -K

验证

- 卸载
ansible webserver -m yum -a 'name="httpd" state=absent' -o -u ops -k -b -K

验证

5. 服务模块
- 重新安装httpd服务
- 调用httpd服务
ansible webserver -m service -a 'name=httpd state=started' -u ops -k -b -K
验证

- 文件模块
- 创建一个文件
ansible webserver -m file -a 'path=/tmp/88.txt mode=777 state=touch' -u ops -k -b -K

验证

- 创建一个目录
ansible webserver -m file -a 'path=/tmp/99 mode=777 state=directory' -u ops -k -b -K
验证

7. 收集模块
- 收集client1的q全部信息
ansible client1 -m setup
- 收集client1的ansible的ipv4信息
ansible client1 -m setup -a 'filter=ansible_all_ipv4_addresses' -k -b -K

8. fetch模块
fetch从远程某主机获取文件到本地
- 参数
dest:用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile;那么在主机中,保存为/backup/master/etc/profile
src:在远程拉取的文件,并且必须是一个file,不能是目录
- 案例
在client1创建一个file23文件
[ops@ansible-client1 ~]$ touch /tmp/file23.txt
[ops@ansible-client1 ~]$ vim /tmp/file23.txt
在server用ansible把client1的file23文件拉取过来
ansible client1 -m fetch -a 'src=/tmp/file23.txt dest=/tmp' -u ops -k -b -K

验证

9. cron模块
- 用ansible远程对client1做任务计划
ansible client1 -m cron -a 'name="sync time from ntpserver" minute="*/10" job="/sbin/ntpdate 172.139.20.210 &> /dev/null"' -u ops -k -b -K

验证

10. group模块
group 用户组模块,添加或删除组
gid 设置组的GID号
name 管理组的名称
state 指定组状态,默认为创建,设置值为absent为删除,present为创建
system 设置值为yes,表示创建系统组
创建组
ansible client1 -m group -a 'name=g1 state=present' -k -b -K

验证

11. script脚本模块
作用:在指定节点运行服务端的脚本
案例:将服务器端上的脚本放在节点机器上运行
(1)在服务器端创建一个脚本
vim wan.sh
#!/bin/bash
date > /tmp/date.log
(2)把脚本分发到各节点执行
ansible webserver -m script -a "/home/ops/wan.sh" -k -o

(3)验证

12. unarchive解压缩模块
默认情况下,此模块会将本地压缩包拷贝到远程机器上解压,当设置了remote_src=yes选项表示解压远程主机上的压缩包
- 相关选项
src:必选项,要解压的包名
dest:必选项,解压到哪个目录下
remote_src:
yes:解压远程主机上的包
no:将管理机上的包传到远程主机上解压(默认就是no)
示例:
- 压缩一个包
sudo tar -cf 111.tar /tmp
[ops@ansible-server ~]$ sudo tar -cf 111.tar /tmp
tar: Removing leading `/' from member names
[ops@ansible-server ~]$ ls
111.tar
- 把压缩包解压到client上
ansible webserver -m unarchive -a 'src=/home/ops/111.tar dest=/tmp' -k
验证

YAML-YAML Ain't Markup Language-非标记语言
- 语法
(1)列表模式
fruits:
- Apple
- Orange
- Strawberry
- Mango
(2)字典模式
martin:
name: Martin D'vloper
job: Developer
skill: Elite
- 示例
需求:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
(1)准备工作
ansible all -m yum -a 'name=httpd state=removed' -o -k -b -K # 清理一下环境
sudo yum install -y httpd
mkdir apache
cd apache/
cp rf /etc/httpd/conf/httpd.conf .
cp -rf /etc/httpd/conf/httpd.conf .
grep '^Listen' httpd.conf Listen 8080 修改配置,用作推送
(2)编写剧本
vim apache.yaml
- hosts: client1
tasks:
- name: install apache packages
yum: name=httpd state=present
- name: copy httpd conf
copy: src=/home/ops/apache/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: ensure is running
service: name=httpd state=started enabled=yes
(3)测试
- 检验语法
ansible-playbook apache.yaml --syntax-check

- 列出任务
ansible-playbook apache.yaml --list-tasks

- 列出主机
ansible-playbook apache.yaml --list-hosts

- 执行
ansible-playbook apache.yaml -b -K

- 验证,要注意端口还有关闭防火墙

(4)handlers
如果配置文件发生变化 Listen 9000
ansible-playbook apache.yaml
vim apache.yaml
如果配置文件再次发生变化 Listen 9080
ansible-playbook apache.yaml 再次执行,配置生效,触发成功


浙公网安备 33010602011771号