常用的ansible模块 ansible 主机 -m 模块 -a “参数”
ping command shell script file copy fetch
lineinfile replace user yum_repository yum(state=present absent)
service lvg lvol
command模块,shell模块
command是默认模块,可以不写。command模块不支持bash的特性会导致任务无法执行,主要换成换成shell模块。
ansible]# ansible node1 -m command -a "uptime" //查看node1主机的CPU负载
ansible]# ansible node1,node2 -m command -a "uptime" //查看两台主机的CPU负载
ansible]# ansible all -m command -a "hostname" //查看所有主机的主机名
ansible]# ansible all -a "date" //command模块是默认模块,可以不写
ansible]# ansible all -a "ip addr show"
ansible]# ansible-doc -l //查看ansible模块
ansible]# ansible-doc -l | wc -l //统计模块总数
ansible]# ansible node1 -m shell -a "who | wc -l"
ansible]# ansible node1 -m shell -a "echo xyz > /opt/xyz"
ansible]# ansible node1 -m shell -a "ls /opt"
script模块
把任务写成脚本然后利用script模块传递到被控主机并执行
[root@control ansible]# vim test.sh #编写测试脚本
#!/bin/bash
yum -y install httpd
systemctl restart httpd
ansible]# ansible all -m script -a "/root/ansible/test.sh" #执行test.sh脚本
ansible]# ansible node5 -m shell -a "ss -ntulp | grep :80" #检查
以上三个模块没有幂等性,重复下达时不能判断而只会重复执行命令。其他很多模块具备幂等性,重复执行显示绿色
[root@control ansible]# ansible node1 -m file -a "path=/opt/abcd src=/var/log state=link"
node1 | CHANGED => { changed 发生创建
"ansible_facts": {...
[root@control ansible]# ansible node1 -m file -a "path=/opt/abcd src=/var/log state=link"
node1 | SUCCESS => { succes 未创建
"ansible_facts": {...
file模块,可以创建、删除、文件,目录
ansible-doc file #查看file模块的帮助文档,去帮助文档查找EXAMPLE相关信息
ansible node1 -m file -a "path=/opt/abc02 state=touch" #创建/opt/abc02文件
ansible node1 -m file -a "path=/opt/abc03 state=directory" #创建/opt/abc03目录
ansible node1 -m file -a "path=/opt/xyz01 state=touch mode=0644"
#创建文件时定义权限是0644,前面第一个数字是0代表没有特殊权限
ansible node1 -m file -a "path=/opt/xyz01 state=touch mode=0644 owner=bin group=sshd" #创建文件时定义owner所属主,和group所属组 path可换成name
ansible node1 -m file -a "path=/opt/xyz state=absent" # absent时代表删除文件(目录)
ansible node1 -m file -a "path=/opt/xyz src=/var/log state=link" #创建软连接文件,src是指定源文件
copy模块,本机传给它机
ansible node1 -m copy -a "src=/etc/hostname dest=/opt" #将控制主机的hostname文件拷贝到被控主机node1的/opt下
ansible-doc copy #查看帮助文档EXAMPLES
fetch模块,它机传到本机
ansible node1,node2 -m fetch -a "src=/etc/hostname dest=/opt" #它机hostname到本机
lineinfile模块 添加行
ansible node1 -m lineinfile -a "path=/opt/hostname line=abc " #默认添加在最后
ansible node1 -m lineinfile -a "path=/opt/hostname line=abc " #重复执行幂等不操作
ansible node1 -m lineinfile -a "path=/opt/hostname line=abc insertafter=con"#含有con字的所有行的最后一行的下面加abc
replace模块,可以替换文档中的关键字串
ansible node1 -m replace -a "path=/opt/hostname regexp=xyz replace=789"
#有xyz的地方换成789,如果出现多个那就都换
ansible node1 -m replace -a "path=/opt/hostname regexp=c replace=789"
#有c的地方换成789,如果出现多个c那就都换
user模块,创建用户,或修改已经存在用户的某参数
ansible all -m user -a "name=abc01" #在所有被控主机创建abc01账户
ansible all -m user -a "name=abc03 uid=1200 group=bin groups=bin,sshd
home=/home/test01"
#在所有被控主机创建账户abc03,并定义uid号1200、基本组是bin,附加组是bin和sshd,家目录是test01,再次执行指令会幂等,但如果随意改变某个参数,比如uid=1100,系统会再次执行任务
ansible all -m user -a "name=abc03 password=123456"
#创建账户配置密码时不能用明文,否则即使创建成功账户也无法用密码123456登录
ansible test -m user -a "name=abc03 password={{'123456'| password_hash('sha512')}}"
#配置密码时要用加密方式
ansible node1 -m user -a "name=abc03 state=absent" #等于userdel abc03,不删家目录
ansible node2 -m user -a "name=abc03 state=absent remove=true"
#等于userdel -r abc03,同时删除家目录
yum_repository模块 配置yum仓库
ansible node1 -m yum_repository -a "name=xyz description=xyzxyz baseurl=http:#1.1.1.1 gpgcheck=0"
#在node1主机配置yum仓库,名字是xyz,描述是xyzxyz,源是1.1.1.1,无需安全检查另外enabled可以不指定,默认enabled=1
ansible node1 -m yum_repository -a "name=xyz state=absent" #删除名字xyz的仓库
ansible node1 -m yum_repository -a "name=xyz description=xyzxyz baseurl=http:#1.1.1.1 gpgcheck=1 gpgkey=xxx" #对已有仓库进行修改
yum模块 装包
ansible node1 -m yum -a "name=vsftpd state=present"
#在node1主机安装vsftpd,state=present是默认值可以不写
ansible node1 -m yum -a "name=vsftpd state=absent" #在node1主机卸载vsftpd
ansible node1 -m yum -a "name=* state=latest"
#对软件进行升级,name后可以写具体软件名称,或者所有软件写*
service模块 控制服务的开关,相比systemctl 命令多加ed
ansible node1 -m yum -a "name=httpd" #利用yum模块安装服务
ansible node1 -m service -a "name=httpd state=started" #开启httpd服务
ansible node1 -m service -a "name=httpd state=stopped" #关服务
ansible node1 -m service -a "name=httpd state=restarted" #重启
ansible node1 -m service -a "name=httpd enabled=yes" #开机自启
ansible node1 -m service -a "name=httpd enabled=no" #取消开机自启
lvg模块 可以配置卷组
ansible node1 -m lvg -a "vg=myvg pvs=/dev/sdb1" #创建myvg的卷组,sdb1(需要分区好)
ansible node1 -m "vgs"
ansible node1 -m lvg -a "vg=myvg pvs=/dev/sdb1,/dev/sdb2" #给卷组增加物理卷空间,原来有ansible node1 -m "vgs"
ansible node1 -m lvg -a "vg=myvg state=absent" #删除卷组
lvol模块 配置逻辑卷
ansible node1 -m lvol -a "lv=mylv size=2G vg=myvg" #利用卷组myvg创建逻辑卷mylv ,大小是2G
ansible node1 -m lvol -a "lv=mylv size=3G vg=myvg" #增加逻辑卷空间变成3G
[root@node1 ~]# lvs #在被控主机查询结果
ansible node1 -m lvol -a "lv=mylv state=absent vg=myvg force=yes" #删除逻辑卷慎用,要加force=yes强制
firewalld模块 配置防火墙,指定服务
---
- hosts: node1
tasks:
- yum:
name: httpd
- service: #控制服务的模块
name: httpd #服务名
state: started #开启
- firewalld: #使用防火墙模块
port: 80/tcp #指定放行服务的端口和协议 也可用service:http指定放行服务
state: enabled # enabled是添加,disabled是删除
permanent: yes #是否永久生效
immediate: yes #是否立刻生效 按ansible-doc firewalld里的example抄
配置sudo提权
创建要提权的账户
ansible all -m user -a "name=alice password={{'123456'|password_hash('sha512')}}"
#为所有被控主机创建账户alice,配置密码123546
ansible all -a "id alice" #查询所有被控主机是否有alice账户
ansible all -m lineinfile -a "path=/etc/sudoers line='alice ALL=(ALL) NOPASSWD:ALL'"
#为所有被控主机的alice账户提升权限 sudo不需要输入密码
[alice@node1 ~]$ sudo systemctl restart sshd #到被控主机测试
修改/root/ansible/ansible.cfg配置文件,默认配置的第107行、第340-344行复制
[root@control ansible]# vim /etc/ansible/ansible.cfg #查看默认的配置文件
第107行、第340-344行复制
[root@control ansible]# cat ansible.cfg #最后被修改完的配置文件
[defaults]
inventory = /root/ansible/inventory
remote_user = alice #连接被控主机的alice账户
[privilege_escalation] #增加权限
become=True #要求使用alice账户时要提权
become_method=sudo #提升权限的操作方式是sudo
become_user=root #提升权限之后以root身份执行任务
become_ask_pass=False #执行指令是是否需要密码
[root@control ansible]# for i in node1 node2 node3 node4 node5 #然后给远程主机传递密钥
do
ssh-copy-id alice@$i #传递密钥给alice
done
测试
ansible all -m yum -a "name=unzip" #尝试装包,可以成功的话说明不但可以远程连接到被控主机的alice账户,而且提升了权限
在远程管理被控主机时,可能连接的不是root账户。如果使用普通账户,那么远程主机就无法执行很多指令,
比如装包、配置、启动服务,就要先修改配置文件为远程主机中的普通账户提升权限
1.[root@control ~]# cat ~/ansible/inventory
2.[test]
3.node1 ansible_ssh_port=端口号 #自定义远程SSH端口
4.[proxy]
5.node2 ansible_ssh_user=用户名 #自定义远程连接的账户名
6.[webserver]
7.node[3:4] ansible_ssh_pass=密码 #自定义远程连接的密码
8.[database]
9.node5
10.[cluster:children]
11.webserver
12.database
ansible常见模块:均可用ansible-doc 查询用法
ping command shell script file copy fetch
lineinfile replace user yum_repository yum(state=present absent)
service lvg lvol
---------------------------------------------------