IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Ansible自动化运维工具最常用命令

Ansible自动化运维工具最常用命令

时间:2023-09-21 19:27:34 来源:IT袋 作者:苏晓敏
导读:Ansible自动化运维工具最常用命令,关于这个Ansible自动化运维工具最常用命令的教程内容,接下来IT袋网小编为大家介绍。 Ansible自动化运维 安装 brew install ansible macos yum install -y ansible centos pip install ansible

Ansible自动化运维工具最常用命令

关于这个Ansible自动化运维工具最常用命令的教程内容,接下来IT袋网小编为大家介绍。

Ansible自动化运维工具最常用命令

Ansible自动化运维

安装

brew install ansiblemacos
yum install -y ansiblecentos
pip install ansiblepython


  • Ansible 官方文档

配置位置

/etc/ansible/ansible.cfg系统范围的配置
~/ansible.cfg用户特定的配置
$pwd/ansible.cfg当前目录下的配置

Inventory文件(hosts列表)

静态Inventory

/etc/ansible/hosts

mail.example.com
[webservers]
foo.example.com
bar.example.com

Inventory 主机组使用多个IP和域名

[web]
172.18.12.5[1:4]
[webservers]
www[01:50].example.com

Inventory 主机组使用子主机组

[usa:children]
southeast
northeast
southwest
northwest

给多台主机设置变量

group variables 如果组中的所有主机共享一个变量值,则可以一次将该变量应用于整个组

[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

命令行(ad-hoc)

ansible

检查Inventory是否生效

$ ansible all --list-hosts

ping所有目标

$ ansible all -m ping

ping本地(不使用SSH连接)

$ ansible all -i localhost, -e '{"ansible_connection": "local"}' -m ping

本地执行命令

$ ansible all -i localhost, -e '{"ansible_connection": "local"}' -a 'hostname'

获取本地主机的信息

$ ansible all -i localhost, -e '{"ansible_connection": "local"}' -m setup

获取远程到本地

$ ansible target -m fetch -a "src=/tmp/seq dest=/tmp/seq"

拷贝本地到远程

$ ansible target -m copy -a "src=/tmp/seq dest=/tmp/seq"

Ansible 命令帮助

$ ansible
$ ansible <host-pattern> [options]

相关阅读

-a MODULE_ARGS, –args=MODULE_ARGS模块参数
–ask-vault-pass询问保险库密码
-B SECONDS, –background=SECONDS异步运行,X 秒后失败 (默认=N/A)
-C, –check不要做任何改变;相反,尝试预测可能发生的一些变化
-D, –diff更改(小)文件和模板时,显示这些文件中的差异; 与 –check 配合使用效果很好
-e EXTRA_VARS, –extra-vars=EXTRA_VARS