啥是ad-hoc,简而言之就是一条命令,执行完即结束,并不会保存。适用于在多台机器查看某个进程活动,或者拷贝指定文件到本地,这种临时的,只需要执行一次的命令。
- #命令+主机名称+'-m'指定模块参数+模块名称+'-a模块动作'+'df -h'具体命令
- [root@Ansible ~]# ansible web01 -m command -a 'df -h'
- web01 | CHANGED | rc=0 >>
- Filesystem Size Used Avail Use% Mounted on
- devtmpfs 476M 0 476M 0% /dev
- tmpfs 487M 0 487M 0% /dev/shm
- tmpfs 487M 7.7M 479M 2% /run
- tmpfs 487M 0 487M 0% /sys/fs/cgroup
- /dev/sda3 19G 2.5G 17G 13% /
- /dev/sda1 197M 110M 88M 56% /boot
- tmpfs 98M 0 98M 0% /run/user/0
绿色:代表没有被管理端主机修改
黄色:代表被管理端主机变更
红色:代表出现故障,注意查看提示
- command # 执行shell命令(不支持管道等特殊字符)
- shell # 执行shell命令
- scripts # 执行shell脚本
- yum_repository # 配置yum仓库
- yum # 安装软件
- copy # 变更配置文件
- file # 建立目录或文件
- service # 启动与停止服务
- mount # 挂载设备
- cron # 定时任务
- get_url #下载软件
- firewalld #防火墙
- selinux #selinux
- [root@Ansible ~]# ansible-doc -l #查看所有可用模块
- [root@Ansible ~]# ansible-doc -copy #查看指定模块方法
- [root@Ansible ~]# ansible-doc -s copy #查看指定模块参数
- #默认模块,执行命令
- [root@Ansible ~]# ansible web_group -a 'hostname'
- web02 | CHANGED | rc=0 >>
- Web02
- web01 | CHANGED | rc=0 >>
- Web01
- #如果需要管道操作,则使用shell
- [root@Ansible ~]# ansible web_group -m shell -a 'ps -ef|grep nginx' -f 50
- #-f 在多台主机运行的并发数量
- #编写脚本
- [root@Ansible ~]# cat yum.sh
- #!/usr/bin/bash
- yum install -y vsftpd
-
- #该脚本文件在本机即可实现远程执行,不需要将脚本文件推送目标主机执行
- [root@Ansible ~]# ansible web_group -m script -a '/root/yum.sh'
- [root@Ansible ~]# ansible web_group -m yum -a "name=httpd state=present"
- name #指定对象
- httpd #软件包名称
- file:// #本地安装路径(相当于yum localinstall本地rpm包)
- http:// #指定yum源(远程仓库获取rpm包)
-
- state #进行的操作
- installed、present #安装软件包
- removed、absent #移除软件包
- latest #安装最新软件包
-
- [root@Ansible ~]# ansible-doc yum
- exclude=kernel*、foo* #排除某些包
- list=ansible #类似于yum list查看是否可以安装
- disablerepo="epel,ol7_latest" #禁用指定的yum仓库
- download_only=true #只下载不安装,类似于yum install d
- name #指定仓库名,没有则用file当仓库文件名
- description #描述
- baseurl #指定yum源
- file #配置文件名,当仓库名与配置文件名不同时候使用
- mirrorlist #镜像列表
- state #行为
- gpgcheck #指定检查秘钥
- enable #是否启用仓库
适用于生产场景,统一管理配置
- #拷贝文件模块
- [root@Ansible ~]# ansible web_group -m copy -a 'src=/etc/passwd dest=/tmp/pass.txt'
-
- #在推送覆盖客户端文件前,对客户端已有文件进行备份(按照时间信息备份)
- [root@Ansible ~]# ansible web_group -m copy -a 'src=/etc/passwd dest=/tmp/pass.txt backup=yes'
-
- #直接向客户端文件中写入数据信息,并且会覆盖客户端文件内原有的数据信息
- [root@Ansible ~]# ansible web_group -m copy -a 'content='123' dest=/tmp/pass.txt'
- [root@Web01 ~]# cat /tmp/pass.txt
- 123
- [root@Ansible ~]# ansible web_group -m copy -a 'content='12345' dest=/tmp/pass.txt'
- [root@Web01 ~]# cat /tmp/pass.txt
- 12345
-
- src #源文件路径
- dest #目标路径
- backup #对推送传输的文件进行备份
- content #添加覆盖内容内容
- group #指定文件属组
- owner #指定文件属主
- mode #指定文件权限
- #创建目录
- [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test state=directory'
-
- #创建文件并指定权限位、属主、属组
- [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test.txt state=touch mode=0555 owner=root group=root'
- [root@Web01 ~]# ll test.txt
- -r-xr-xr-x 1 root root 0 Apr 17 15:43 test.txt
-
- #客户端创建软链接
- [root@Ansible ~]# ansible web_group -m file -a 'src=/root/test path=/root/test_link state=link'
-
- #创建目录,指定属组、属主、权限授权位,并进行递归授权
- [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test state=directory owner=www group=www mode=0700 recurse=yes'
-
- path #指定远程主机目录或文件信息
- recurse #递归授权
- state
- directory #在客户端创建目录
- touch #在客户端创建文件
- link #link表示创建软链接文件
- absent #表示删除文件或目录
- mode #设置文件或目录权限
- owner #设置文件或目录属主信息
- group #设置文件或目录属组信息
- [root@Ansible ~]# ansible web_group -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.0-1.el7.x86_64.rpm dest=/root mode=0644'
-
- url #指定下载地址
- dest #指定客户端下载目录
- mode #指定下载文件权限
- checksum #检验加密算法
- md5
- sha256
- #启动crond并加入开机自启动
- [root@Ansible ~]# ansible web_group -m service -a 'name=crond state=started enable=yes'
- [root@Ansible ~]# ansible web_group -m systemd -a 'name=crond state=started enable=yes'
-
- #停止crond并删除开机自启动
- [root@Ansible ~]# ansible web_group -m service -a 'name=crond state=stoped enable=no'
- [root@Ansible ~]# ansible web_group -m systemd -a 'name=crond state=stoped enable=no'
-
- name #定义要启动服务的名称
- state #指定服务状态
- started #启动服务
- stopped #停止服务
- restarted #重启服务
- reloaded #重载服务
- enabled #开机自启动
- yes
- no
Ansible管理用户与组,通常使用user、group模块
- [root@Ansible ~]# ansible web_group -m group -a 'name=www gid=888'
-
- name #组名
- gid #组gid
- state
- absent #移除客户机组
- present #创建客户机组(默认)
- #创建用户,指定uid、gid,不创建家目录,不允许用户登录
- [root@Ansible ~]# ansible web_group -m user -a 'name=www uid=888 group=www shell=/sbin/nologin create_home=false'
-
- #创建用户并生成秘钥对
- [root@Ansible ~]# ansible web_group -m user -a 'name=www uid=888 group=www shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'
-
- #将明文密码加密,然后进行用户创建
- [root@Ansible ~]# ansible web_group -m debug -a "{{'koten'|password_hash('sha512','salt')}}"
-
- #创建用户
- [root@Ansible ~]# ansible web_group -m user -a 'name=koten password=xxxx create_home=true shell=/bin/bash'
-
- uid #指定用户uid
- group #指定用户组名称
- groups #指定附加组名称
- password #给用户添加密码(-a后需要用单引号)
- shell #指定用户登录shell
- create_home #是否创建家目录
- yes
- false
- #正常使用crond服务
- [root@Ansible ~]# crontab -l
- no crontab for root
-
- #使用ansible添加一条定时任务
- [root@Ansible ~]# ansible web_group -m cron -a "minute=* hour=* day=* mounth=* weekday=* job='/bin/sh /root/test.sh'"
-
- #不定义默认每分钟执行一次
- [root@Ansible ~]# ansible web_group -m cron -a "job='/bin/sh /root/test.sh'"
-
- #设置定时任务注释信息,可以用name设定
- [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh'"
-
- #删除相应定时任务
- [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh' state=absent"
-
- #注释相应定时任务,使定时任务失效
- [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh' disabled=no"
- #指定源目录与挂载目录,挂载类型,挂载方式,传递给挂载命令的参数
- [root@Ansible ~]# ansible web_group -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
-
- opts #传递给挂载命令的参数
- present #开机挂载,仅将挂载配置写入/etc/fstab
- mounted #挂载设备,并将配置写入/etc/fstab
- unmounted #卸载设备,不会清除/etc/fstab写入的配置
- absent #卸载设备,会清理/etc/fstab写入的配置
- #修改配置文件关闭selinux,重启后生效
- [root@Ansible ~]# ansible web_group -m selinux -a 'state=disabled'
-
- #临时关闭selinux
- [root@Ansible ~]# ansible web_group -m shell -a 'setenforce 0'
-
- #获取selinux当前状态
- [root@Ansible ~]# ansible web_group -m shell -a 'getenforce'
- #开启http服务的防火墙,并设置为开机自启动
- [root@Ansible ~]# ansible web_group -m firewalld -a 'service=http permanent=yes state=enabled'
-
- #开启http服务的防火墙,并设置为开机自启动,临时生效
- [root@Ansible ~]# ansible web_group -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
-
- 开启端口8080-8090端口tcp协议的防火墙,并设置为开机自启动,临时生效
- [root@Ansible ~]# ansible web_group -m firewalld -a "port=8080-8090/tcp immediate=yes permanent=yes state=enabled"
-
- service #指定开放或关闭的服务名称
- port #指定开放或关闭的端口
- permanent #是否添加永久生效
- state #开启或者关闭
- enabled
- disabled
- zone #指定配置某个区域
- rich_rule #配置辅规则
- masquerade #开启地址伪装
- immediate #临时生效
- source #指定来源IP
在以下场景中会经常用到主机信息
1、根据不同主机不同IP创建对应不同的IP目录
2、根据不同主机不同主机名创建对应主机名的目录
3、自动化运维平台需要自动获取到主机的IP地址,内存信息,磁盘信息,主机名等等
4、如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G,写playbook的话,可以获取对应主机的内存做出计算,写判断进行内存分配。
[root@Ansible ~]# ansible Web01 -m setup
[root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_default_ipv4'
[root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_fqdn'
[root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_memory_mb'
[root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_devices'
- 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个数(只显示总的个数)。
注意:此处的匹配规则支持通配符,后面我们用playbook,会参考这些内置变量使用
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!