admin管理员组

文章数量:1531426

Ansible的常用模块

文章目录

  • Ansible的常用模块
    • ansible的执行
    • ansible ad-hoc
    • ad-hoc的命令模式(语法)
    • ac-hoc返回结果的颜色
    • ansible帮助手册
    • ansible模块:command
    • ansible模块:shell
    • ansible模块:script
    • ansible软件管理模块:yum
    • ansible软件管理模块:yum_repository
    • ansible文件管理模块:copy
    • ansible文件管理模块:file
    • ansible文件管理模块:get_url
    • ansible服务管理模块:service
    • ansible用户管理模块:user
    • ansible用户管理模块:group
    • ansible定时任务模块:cron
    • ansible磁盘挂载模块:mount
    • ansible防火墙模块:selinux
    • ansible防火墙模块:firewalld
    • ansible解压模块:unarchive
    • ansible自动化运维模块:setup

ansible的执行

ad-hoc
playbook

ansible ad-hoc

ad-hoc:临时的命令,执行后结束,不会保存
使用场景
比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等

ad-hoc的命令模式(语法)

语法:
ansible 主机名(主机清单中的主机名或者主机组) -m 模块名 [-a 动作]

ac-hoc返回结果的颜色

绿色:被管理端执行成功,并且结果不会发生改变
黄色:被管理端执行成功,但是结果是变化的
红色:执行失败,注意看报错

ansible帮助手册

ansible-doc 模块名 

ansible模块:command

ansible web_group -m command -a '命令(不带特殊符号)'

ansible模块:shell

ansible web_group -m shell -a '命令'

ansible模块:script

# ansible远程执行脚本
ansible web_group -m script -a '/root/a.sh'

ansible软件管理模块:yum

yum
- name:
- 直接指定包名:从仓库安装   yum install httpd
- http://   从指定URL安装 yum install http://mirrors.aliyun/epel/http-
1.1.rpm
- file://   从本地安装 类似 yum localinstall /root/http-1.1.rpm
- state
- absent:卸载
- present:安装
- lastest:
- download_only:
- true:只下载不安装
- false:下载并安装
-list
- 包名
"yumstate": "available" 证明包可以安装但是没装
"yumstate": "installed" 已经安装了
exclude=kernel*,foo*       #排除某些包
list=ansible           #类似于yum list查看是否可以安装
disablerepo="epel,ol7_latest"  #禁用指定的yum仓库

ansible软件管理模块:yum_repository

## 创建新的仓库配置文件
ansible db01 -m yum_repository -a 'name=local description=xxx baseurl=file:///mnt
gpgcheck=no enabled=yes'
## 创建新的仓库配置,并且文件名和仓库名不同
ansible web01 -m yum_repository -a 'name=local file=zls_local description=xxx
baseurl=file:///mnt gpgcheck=no enabled=yes'
## 追加仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansible
description=xxx baseurl=http://www.baidu'
## 删除仓库
ansible lb01 -m yum_repository -a 'name=test2_ansible file=test_ansible state=absent'
yum_repository
- name:仓库名字(如果有file,只是仓库名,如果没有file,文件名和仓库名)
- file:指定仓库的文件名
- description:仓库的描述(name)
- baseurl:仓库的url
- gpgcheck:
- no:不开启 0
- yes:开启 1(默认)
- enabled:
- no:不开启 0
- yes:开启 1 (默认)

ansible文件管理模块:copy

# 远程推送文件
ansible nginx -m copy -a 'src=/etc/passwd dest=/root'
copy
- src:指定源文件的路径
- dest:指定目标路径
- owner:指定属主
- group:指定属组
- mode:指定权限
- backup:
- yes:如果目标路径,存在同名文件,就将目标文件备份
- no:不备份直接覆盖(默认)
- content:将指定文本内容覆盖到目标文件中
- remote_src:将命令变成cp
- yes:将源文件,编程远端的源文件(ansible被管理端)
- no:源文件还是本地文件(ansible管理端 默认)
- follow:拷贝软连接
- yes:会将软连接一起拷贝
- no:会生成一个新的软连接文件

ansible文件管理模块:file

path:要创建的文件路径
src:链接的源文件(软链接、硬链接)
dest:链接的目标文件(软链接、硬链接)
state:
link:软链接
hard:硬链接
touch:将path当成普通文件创建出来
directory:将path当成目录创建出来
absent:删除指定的文件
file:修改path指定的文件属性
recurse:
yes:递归创建
no:默认
owner:属主
group:属组
mode:权限
# 1.创建普通文件
[root@m01 ~]# ansible all -m file -a 'path=/root/test_ansible.txt state=touch'
# 2.创建目录
[root@m01 ~]# ansible all -m file -a 'path=/root/test_dir state=directory'
# 3.递归创建目录(默认支持)
[root@m01 ~]# ansible all -m file -a 'path=/root/test_dir state=directory'
# 4.recurse 新创建的目录都会修改指定权限,目录中已存在的文件不会被修改
yes:递归修改所有目录下的所有文件权限
no:不改(no)
[root@m01 ~]# ansible web01 -m file -a 'path=/root/1 state=directory owner=www
group=www recurse=yes'
# 5.删除目录
[root@m01 ~]# ansible web01 -m file -a 'path=/root/1 state=absent'
# 6.软链接
[root@m01 ~]# ansible web01 -m file -a 'src=/app/nginx-1.20.1 dest=/app/nginx
state=link'

ansible文件管理模块:get_url

url:指定安装包下载url
dest:指保存的路径
mode:下载下来后的权限

[root@m01 ~]# ansible web01 -m get_url -a
'url=http://test.driverzeng/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz dest=/tmp
owner=www group=www mode=0755'

ansible服务管理模块:service

name:服务名
state:
started:启动
stopped:停止
restarted:重启
reloaded:重新加载配置文件
enabled:
yes:允许开机自启
no:默认

ansible用户管理模块:user

name:用户名
uid:指定uid  -u
group:指定gid或者组名  -g
groups:指定附加组  -G
append:追加,配合-G使用  -a
shell:指定登录的shell  -s
comment:指定描述  -c
create_home:是否创建家目录
   yes:创建(默认)
   no:不创建
generate_ssh_key:是否创建秘钥对
   yes:创建
   no:不创建(默认)
ssh_key_bits:私钥长度
ssh_key_file:私钥的位置
state:
   present:创建用户(默认)
   absent:删除用户
remove
   yes:将用户的家目录等。。。全部删除  -r
   no:默认


# 创建tjh用户,并生成公钥和私钥
[root@m01 ~]# ansible web01 -m user -a 'name=tjh uid=69 generate_ssh_key=yes
ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'


db01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1000, 
    "home": "/home/tjh", 
    "name": "tjh", 
    "shell": "/bin/bash", 
    "ssh_fingerprint": "2048 SHA256:2AixAKsBJ6QEEtVC3Lu8EzJPPu64IjyrH7bxuFzLtc8 ansible-generated on db01 (RSA)", 
    "ssh_key_file": "/home/tjh/.ssh/id_rsa", 
    "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkPQ33rEmDBEqBLtU5l/NUWbjAM2N0RtvltKCrYqPK7tayAO7KUJO8lbxIg1NQE6ax4T+6NtwVLigP2r9zl09qZ8VH17zf4Nl8n9wMg11ZYTaPryUJQAsF9ZUbQgzAEQdHQUxfYBgVjiSdT/1/Rtz4UszB4ZirIJvaK2Hdc03wOCBy1D5C5UwF+w4QEqSvkyi1eHXnlLVDlvUCiHrpafC444LOkypZRvuUNDtSuASPH9RafVLps3Cxjd5Otze97Ttg6B6Fo6dfqnzRooJV1B56FwLyaNg67gy6errbLpHWax37IZsv1F+2dgzzIHsWs3AiPbAtnLT+LIaykcHVBLEd ansible-generated on db01", 
    "state": "present", 
    "system": false, 
    "uid": 69

创建mysql 禁止登陆, 不创建家目录
[root@m01 ~]# ansible web01 -m user -a 'name=mysql shell=/sbin/nologin create_home=no'

ansible用户管理模块:group

name:指定组名
gid:指定gid
state:
prensent:创建组(默认)
absent:删除组
[root@m01 ~]# ansible all -m group -a 'name=suibian gid=333 state=present'

ansible定时任务模块:cron

name:注释
分:minute
时:hour
日:day
月:month
周:weekday
job:要执行的任务
state:
present:创建定时任务
absent:删除定时任务(删除任务要根据name)
user:指定定时任务的用户

[root@m01 ~]# ansible all -m cron -a "name='时间同步' minute=*/5 job='/sbin/ntpdate
time1.aliyun &>/dev/null'"

ansible磁盘挂载模块:mount

path:指定挂载的路径
src:被挂载的路径
fstype:指定文件系统类型
state:
prsent:开机挂载,只写入/etc/fstab
[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploads
src=172.16.1.31:/data/wp_data fstype=nfs state=present'
absent:卸载设备,会清理/etc/fstab写入的配置
[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploads
state=absent'
mounted:既挂载又写入/etc/fstab文件中
[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploads
src=172.16.1.31:/data/wp_data fstype=nfs state=mounted'
unmounted:卸载设备,不会清除/etc/fstab写入的配置
opts:指定挂载的权限
[root@m01 ~]# ansible web01 -m mount -a 'path=/code/wordpress/wp-contend/uploads
state=absent'

ansible防火墙模块:selinux

state:
disabled 关闭selinux
root@m01,172.16.1.61:~ # ansible db01 -m selinux -a 'state=disabled'
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change
will take effect next reboot.
db01 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "configfile": "/etc/selinux/config", 
    "msg": "Config SELinux state changed from 'enforcing' to 'disabled'", 
    "policy": "targeted", 
    "reboot_required": true, 
    "state": "disabled"
}

ansible防火墙模块:firewalld

service:根据服务放行端口
port:
端口/tcp
state
disabled:禁用端口
enabled:放行指定端口
[root@m01 ~]# ansible lb02 -m firewalld -a 'port=7001/tcp state=enabled'
[root@m01 ~]# ansible lb02 -m firewalld -a 'service=http state=enabled'

ansible解压模块:unarchive

src:指定源文件
dest:目标路径
remote_src:
yes:压缩包在远端机器上
no:默认 ,压缩包在管理端
注意:前提条件是,被管理端上必须有所有的解压命令
[root@m01 tmp]# ansible lb_group -m unarchive -a 'src=/tmp/pymp-dmy45m_h.zip
dest=/usr/local/src'

ansible自动化运维模块:setup

[root@m01 tmp]# ansible all -m setup -a 'filter=ansible_all_ipv4_addresses'
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)

本文标签: 模块常用ansible