ansible 简单命令以及常用模块

一、ansible 简单命令

ansible ansible-doc ansible-playbook ansible-vault ansible-console ansible-galaxy ansible-pull

1、ansible-doc 显示模块帮助

ansible-doc [options][module]
-a  显示所有模块文档
-l,--list 列出可用模块
-s,--snippet 显示指定模块的playbook片段
实例:
ansible-doc -l 列出所有模块
ansible-doc ping 查看指定模块的帮助用法
ansible-doc -s ping 查看指定模块的帮助用法
ansible通过ssh实现配置管理、应用部署,任务执行等功能,建议配置ansible段能基于密钥认证的方式联系各被管理节点

2、ansible [host-pattern] [-m module_name] [-a args]

--version 显示版本
-m module 指定模块,默认为command
-v 详细过程 -vv -vvv 更详细
--list-host 显示主机列表,可简写--list
-k ,--ask-pass 提示输入ssh连接密码。默认key验证
-K, --ask-become-pass  提示输入sudo时的口令
-C,--check 检查不执行
-T --timeout=TIMEOUT 执行命令的超时时间,默认10s
-u --user=REMOTE——USER 执行远程执行的用户
-b, --become 代替旧版本的sudo切换

例:查看192.168.1.184主机下的/etc/hosts文件
	ansible 192.168.1.184 -k -m command -a'cat /etc/hosts'

3、ansible的Host-pattern 匹配模式

匹配主机的列表

all:表示所有Inventory中的所有主机

*:通配符
    ansible “*” -m ping
    ansible 192.168.1.* -m ping
    ansible “*srvs” -m ping

或关系
    ansible “webserver:dbserver” -m ping
    ansible "webserver:dbserver" -m ping #执行在web组并且在dbserver组中的主机(忽略重复的)

与关系
    ansible "webserver:&dbserver" -m ping
    只执行在web组并且也在dbserver组中的主机

逻辑非
    ansible 'webserver:!dbserver' -m ping  【注意此处只能使用单引号!】

综合逻辑
    ansible 'webserver:dbserver:&webserver:!dbserver' -m ping

正则表达式
    ansible "webserver:&dbserver" -m ping
    ansible "~(web|db).*\.magedu.\com" -m ping

4、ansible命令执行过程

a 加载自己的配置文件 默认/etc/ansible/ansible.cfg
b 加载自己对应的模块 如command
c 通过ansible将模块或命令生成对应的临时py文件,并将改文件传输至远程服务器的对应执行用户SHOME/.ansible/tmp/ansible-tmp-数字/XXX.py文件
d 文件加+x执行
e 执行并返回结果
f 删除临时py文件,sleep 0退出

5、执行状态

绿色:执行成功并且不需要做改变的操作
黄色:执行成功并且对目标主机做变更
红色:执行失败

在这里插入图片描述

6. ansible-galaxy

链接:https://galaxy.ansible.com下载对应的role

列出所有已安装的galaxy
ansible-galaxy list
安装galaxy
ansible-galaxy install geerlingguy.redis
删除galaxy
ansible-galaxy remove geerlingguy.redis

7. ansible-pull

推送命令至远程,效率无限提升,对运维要求较高

8. ansible-playbook

ansible-playbook hello.yml    【书写注意空格,行首注意对齐】
cat hello.yml 
---
# hello test yml file
- hosts: all
  remote_user: root    #远程用户
  tasks:
        - name: look hostname
          command: hostname

详细讲解见:https://blog.csdn.net/weixin_42881588/article/details/107063574

8. ansible-vault 管理加密解密yml文件

ansible-vault [create | decrypt | edit | encrypt | rekey | view]
ansible-vaullt encrypt hello.yml 加密
ansible-vault decrypt hello.yml 解密
ansible-vault view hello.yml 查看
ansible-vault edit hello.yml 编辑加密文件
ansible-vault rekey hello.yml 修改口令
ansible-vault create new.yml 创建新文件

9. ansible-console:2.0+新增,可交互执行命令,支持tab

root@test(2)[f:10] $
执行用户@当前操作的主机组(当前组的主机数量)[f:并发数]$
设置并发数:fock n 例如:fock 10
切换组:cd 主机组 例如:cd webserver 
列出当前组的主机列表:list
列出所有内置命令:?或help
示例:
root@all (2)[f:5]$ list
root@all (2)[f:5]$ cd webserver
root@dbserver (2)[f:5]$ list
root@dbserver (2)[f:5]$ yum name=httpd state=present
root@dbserver (2)[f:5]$ service name=httpd state=restart

二、ansible常见模块

ping:

command:在远程主机执行命令,默认模块。可忽略-m选项
    ansible srvs -m command -a ‘systemctl restart sshd’
    ansible srvs -m command -a 'echo magedu | passwd --stdin wang '不成功
    此命令不支持$VRNAME< >  | ; & 等,需要用shell模块实现

shell:和command相似,用shell执行命令
    ansible srv -m shell -a ‘echo magedu | passwd --stdin wang’
    调用bash执行命令 类似cat /tmp/stanley.md | awk -F '|' '{print $1,$2}' & >
    /tmp/example.txt 这些复杂命令,及时使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器

script:运行脚本
    -a “/PATH/TO/SCRIPT_FILE”
    ansible webserver -m script -a f1.sh

copy:从服务器复制文件到客户端
    ansible all -m copy -a 'src=/data/test1 dest=/data/test1 backup=yes mode=000 owner=zhang'  ##如目标存在,默认覆盖,此处是指先备份,并修改全向属主
    ansible all -m shell -a 'ls -l /data/'
    ansible all -m copy -a "content='test content\n' dest=/tmo/f1.txt" 利用内容,直接生成目标文件

fetch:从客户端取文件至服务器端,与copy相反,目录可以先tar
    ansible all -m fetch -a ‘src=/root/a.sh dest=/data/f2.sh'

在这里插入图片描述

file:设置文件属性(状态,属组,属主,权限)
	ansible all -m file -a “path=/root/a.sh owner=zhang mode=755”
	ansible all -m file -a 'src=/data/test1 dest=/tmp/test state=link'
	ansible all -m file -a ’name=/data/f3 state=touch‘  #创建文件
	ansible all -m file -a ’name=/data/f3 state=absent‘ #删除文件
	ansible all -m file -a ’name=/data state=directory‘ #创建目录
	ansible all -m file -a ’src=/etc/fstab dest=/data/fstab.link state=link‘

archive打包模块
unarchive 解打包模块

hostname 管理主机名
    ansible 192.168.10.24 -m hostname -a “name=kso-bj6-zw-zhangwei”#永久生效(但hosts文件需要手动更改)

cron 计划任务
    支持时间:minute,hour,day,month,weekday
    ansible all -m cron -a "minute=*/5 weekday=1,3,5 job='/usr/sbin/ntpfata 172.16.0.1 & >/dev/null' name=Synctime" 创建任务
    ansible all -m cron -a "disabled=true job='/usr/sbin/ntpfata 172.16.0.1 & >/dev/null' name=Synctime" 禁用任务(加#号注释)
    ansible all -m cron -a "disabled=no  job='/usr/sbin/ntpfata 172.16.0.1 & >/dev/null' name=Synctime" 启用任务
    ansible all -m  cron -a 'state=absent name=Synctime' 删除任务

yum:管理包
    ansible all -m yum -a 'name=httpd state=latest'安装
    ansible all -m yum -a 'name=httpd state=ansent' 卸载
    ansible all -m yum  -a 'name=dstat update_cache=yes' 更新缓存
    【注:dstat--监控工具https://www.jianshu.com/p/49b259cbcc79】

service:管理服务
    ansible all -m service -a 'name=httpd state=stopped'
    ansible all -m service -a 'name=httpd state=started enabled=yes'
    ansible all -m service -a 'name=httpd state=reload'
    ansible all -m service -a 'name=httpd state=restart'

user:管理用户
    ansible all -m user -a 'name=user1 comment="test user" uid=2048 home=/data/home/user1 group=root'  创建用户,以及uid,家目录,并描述(comment)
    ansible all -m user -a 'name=zhangwei shell=/sbin/nologin  system=yes home=/data/home/zhangwei'    创建不可登陆的系统用户
    ansible all -m user -a 'name=zhangwei state=absent remove=yes'删除用户及家目录

group:管理组
    ansible all -m group -a "name=testgroup system=yes"
    ansible all -m group -a "name=testgroup state=absent"
    
ansible-doc -s moudul #简短介绍模块使用方法
ansible-doc modul  #详细介绍模块使用方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值