温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ansible常用命令使用

发布时间:2020-07-06 17:26:22 来源:网络 阅读:450 作者:simon_ymc 栏目:系统运维
  1. ansible命令格式

# 查看ansible帮助

ansible -h

# 命令格式

Usage: ansible <host-pattern> [options]


-a MODULE_ARGS  # 模块参数

-C, --check  # 干跑,白跑

-f FORKS, --forks=FORKS  # 指定并发,默认5个

--list-hosts  # 列出主机

-m MODULE_NAME  # 模块名称

--syntax-check  # 检查语法

-k  # 密码


2.ansible链接机器

rpm -ql ansible|more # 查看ansible生成的文件


/etc/ansible

/etc/ansible/ansible.cfg  # 配置文件

/etc/ansible/hosts  # 配置管控机

/etc/ansible/roles  # 空文件夹

# 在hosts文件中将主机地址一个个添加进去


ansible 主机地址 -m ping -k  # ansible底层通过ssh实现


# 生成密钥

ssh-keygen

# 将公钥拷贝至目标主机

ssh-copy-id root@主机地址

# 通过ssh即可链接目标主机

ssh 'root@主机地址'

exit退出


# 完成上面的操作后 再去探测机器是否在线就不要再加-k

ansible 主机地址 -m ping


"""测试机器是否在线的方式及分组管理"""

ansible 192.168.226.101 -m ping  # 单独机器的ping

ansible 192.168.226.101,192.168.226.102 -m ping  # 多个机器的ping

ansible all -m ping  # 全部机器

# 分组:比如有N多台机器,有些是负责数据库的,有些是uwsgi的,有些是redis的等

ansible web -m ping  # 单个的组

ansible web,db -m ping  # 多个组的并集

ansible 'web:&db' -m ping  # 多个组的交集  必须是单引号 双引号不行

ansible 'web:!db' -m ping  # 多个组的差集,在前面但是不在后面


3.ansible模块介绍

ansible-doc -l |wc -l  # 查看ansible总模块数


4.ansible-doc 命令格式

-j  # 以json的方式返回数据

-l, --list  # 列出所有的模块

-s, --snippet # 以片段式显示模块信息 

# 常用命令

ansible-doc -s 模块名  # 查看模块帮助信息


模块介绍

  • command 

分组机器批量执行系统命令


ansible-doc -s command


ansible web -m command -a "pwd"

ansible web -m command -a "ls"

ansible web -m command -a "chdir=/tmp pwd" #切换目录并执行命令

ansible web -m command -a "creates=/tmp pwd" #因为tmp目录存在,pwd不会执行

ansible web -m command -a "creates=/tmp2 pwd" #因为tmp2不存在,pwd执行

ansible web -m command -a "removes=/tmp2 pwd" #因为tmp2不存在pwd不执行

ansible web -m command -a "removes=/tmp pwd" #因为tmp目录存在,pwd会执行


# 创建用户

ansible web -m command -a 'useradd jason'

# 设置密码(本机设置)  并需二次确认

passwd jason

# 设置密码(本机设置)  无需二次确认

echo "123" |passwd --stdin jason #设置用户的密码



  • shell

当命令中包含$home和常见符号(<,>,|,;.&)等的时候,需要使用shell才能生效


ansible-doc -s shell


ansible web -m shell -a "echo '123' |passwd --stdin jason"

ansible web -m shell -a "chdir=/tmp pwd" shabang

ansible 192.168.226.101 -m shell -a "bash a.sh" #执行shell脚本

ansible 192.168.226.101 -m shell -a "/root/a.sh" # 执行shell脚本,文件要有执行的权限

ansible 192.168.226.101 -m shell -a "/root/a.py" #执行Python文件


  • script

在远程机器上执行本地脚本

ansible-doc -s script

ansible db -m script -a "/root/a.sh"  # 执行本地的文件,管控机的文件

ansible db -m script -a "creates=/root/a.sh /root/a.sh"  # 判断被控机上的文件是否存在,如果不存在,就执行,如果存在,就跳过

ansible db -m script -a "creates=/tmp /root/a.sh"  # 判断被控机上的文件


  • copy

将本地的文件拷贝到远程机器

ansible-doc -s copy


backup  # 创建一个备份文件,以时间戳结尾

content  # 直接往文件里面写内容

dest  # 目标地址

group  # 属组

mode  # 文件的权限 W 2 R 4 X 1

owner  # 属主

src  # 源地址

ansible web -m copy -a "src=/etc/fstab dest=/tmp/f"  # 复制本地文件到远程主机,并修改文件名,多次执行不会改变,因为checksum值是一样的

ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes"  # 复制本地文件,并备份

ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes group=alex mode=755"  # 复制本地文件到远程主机,并指定属组和权限

ansible web -m copy -a "src=/etc/init.d dest=/tmp backup=yes group=alex mode=755"  # 复制本地的目录到远程主机,修改目录权限,则目录里面的文件也会跟着变更

ansible web -m copy -a "src=/etc/init.d/ dest=/tmp backup=yes group=alex mode=755"  #复制本地目录下的所有文件,

ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语,嘈嘈切切错 杂弹,大珠小珠落玉盘' dest=/tmp/b"  # 直接往文件里面写内容,覆盖写,慎用


  • File

文件属组、权限、硬软链等


group  # 属组

mode  # 权限

owner  # 属主

path  # 路径

src  # 软硬链接

state =link

state =hard

state

directory  # 目录

file  

touch  # 空文件

absent  # 删除

link  # 软连接

hard  # 硬链接

  

ansible web -m file -a "path=/jason5 state=directory owner=jason" #创建目录,并制定属主

ansible web -m file -a "path=/tmp/jason.txt state=touch mode=777" #创建文件,并指定权限

ansible web -m file -a "path=/tmp/cron src=/var/log/cron state=link" #创建软链接,链接的是自己的文件

ansible web -m file -a "path=/tmp/cron state=absent" # 删除软连接

ansible web -m file -a "path=/jason5 state=absent" #删除文件夹



软连接   快捷方式(windows)   ln -s   源文件修改软连接修改     源文件删除软连接失效    可以跨分区 

硬链接   硬盘的位置 ln       源文件修改硬链接修改        源文件删除硬链接不变    不可以跨分区

复制     开辟新空间 cp       源文件修改cp的不变         源文件删除不变       可以跨分区


  • fetch

拉取远程主机文件

dest 目标地址

src 源地址

ansible web -m fetch -a "src=/var/log/cron dest=/tmp"  # 拉取远程主机的文件,并以主机ip地址或者主机名为目录,并且保留了原来的目录结构


  • yum模块

ansible-doc -s yum


disablerepo  # 禁用某个源

enablerepo  # 启用某个源

name  # 包名

state

install

remove

yum install -y python2-pip  # 在epel源中

# 将主机中下载好的pip拷贝到其他被管控机上

ansible all -m copy -a 'src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo'


ansible web -m yum -a "name=python2-pip"  # 安装单个软件包


# 查看是否安装 对应机器上执行下述命令

rpm -q 包名

# eg:rpm -q pip/python-pip/python2-pip/redis


ansible web -m yum -a "name=python2-pip,redis"  # 安装多个软件包

ansible web -m yum -a "name='@Development Tools'"  # 安装包组

ansible web -m yum -a "name=nginx state=absent"  # 卸载


  • pip模块

python安装第三方模块的工具


pip freeze > requirements.txt   # 将本地环境导出到某个文件中,文件中不写版本信息默认安装最新版本

pip install -r requirements.txt  # 安装所有的包(******)


pip list  # 查看所有的包

pip uninstall flask  # 卸载


# 下载包

Python setup.py build 

python setup.py install


  • pip模块

ansible-doc -s pip


chdir  # 切换目录

name  # 包名

requirements  # 导出的文件

virtualenv  # 虚拟环境


# 安装django(时间较长~)

ansible web -m pip -a "name=django==1.11"


  • service模块

ansible-doc -s service

# 关键参数

enabled #开机启动

name #服务名称

state  

    started

    stopped

    restarted

    reloaded

ansible web -m service -a "name=redis state=started" #启动

ansible web -m service -a "name=redis state=stopped" #关闭

ansible web -m service -a "name=redis enabled=yes" #设置开机自启动


  • cron计划任务模块

# 格式

* * * * * job

分 时 日 月 周 任务


ansible-doc -s cron


day  # 天

disabled  # 禁用crontab,表现形式加#

hour  # 小时

job  # 任务

minute  # 分钟

month  # 月

name  # 名字,描述信息

user  # 用户

weekday  # 周


# 添加时名字必须不同,不加名称为None(crontab -l查看计划任务)

ansible web -m cron -a "minute=12 name=touchfile job='touch /tmp/xiaoqiang.txt'"  # 创建

ansible web -m cron -a "name=touchfile state=absent"  # 删除

ansible web -m cron -a "minute=12 name=touchfile2 job='touch /tmp/jason.txt' disabled=yes"  # 注释

ansible web -m cron -a "name=None state=absent"  # 删除名称为空的计划任务


  • user模块

ansible-doc -s user


group # 属组

groups  # 附加组

home  # 设置家目录

name  # 用户名

remove  # 删除用户并删除用户的家目录

shell  # 用户登录后的shell

system  # 系统用户

uid  # 用户的id

ansible web -m user -a "name=alex10 shell=/sbin/nologin home=/opt/alex10 uid=3000 groups=root" #创建用户,并指定用户的shell,家目录,uid,以及附加组

ansible web -m user -a "name=alex11 shell=/sbin/nologin home=/opt/alex11"

ansible web -m user -a "name=alex12 system=yes" #创建系统用户

ansible web -m user -a "name=alex12 state=absent" #删除用户,单不删除家目录

ansible web -m user -a "name=alex11 state=absent remove=yes" # 删除用户并删除用户的家目录


  • group模块

ansible-doc -s group


gid  # 组id

system  # 系统组

name  # 名称

ansible web -m group -a "name=jason10 system=yes gid=5000"  # 创建系统组

ansible web -m group -a "name=jason11"  # 创建普通的组

ansible web -m group -a "name=jason11 state=absent"  # 删除组


  • setup模块

收集远程主机的一些基本信息

# 查看详细信息

ansible 主机名 -m setup|more


ansible_all_ipv4_addresses  # 所有的ipv4地址

ansible_all_ipv6_addresses  # 所有的ipv6地址

ansible_architecture  # 系统的架构

ansible_date_time  # 系统时间

ansible_default_ipv4  # 默认的ipv4地址

address  # ip地址

alias  # 网卡名称

broadcast  # 广播地址

gateway  # 网关

netmask  # 子网掩码

network  # 网段

ansible_default_ipv6  # 默认的ipv6地址

ansible_device_links  # 系统的磁盘信息

ansible_distribution  # 系统名称

ansible_distribution_file_variety  # 系统的基于公司

ansible_distribution_major_version  # 系统的主版本

ansible_distribution_version  # 系统的全部版本

ansible_dns  # 系统的dns 默认udp 端口53

ansible_domain  # 系统的域 ldap

ipv4  # ipv4地址

ansible_env  # 系统的环境

ansible_fqdn  # 系统的完整主机名

ansible_hostname  # 系统的简写主机名

ansible_kernel  # 系统的内核版本

ansible_machine  # 系统的架构

ansible_memtotal_mb  # 系统的内存

ansible_memory_mb  # 系统的内存使用情况

ansible_mounts  # 系统的挂载信息

ansible_os_family  # 系统家族

ansible_pkg_mgr  # 系统的包管理工具

ansible_processor  # 系统的cpu

ansible_processor_cores  # 每颗cpu的核数

ansible_processor_count  # cpu的颗数

ansible_processor_vcpus  # cpu的个数=cpu的颗数*每颗cpu的核数

ansible_python  # 系统python信息

ansible_python_version  # 系统python的版本

ansible_system  # 系统名字


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI