Ⅰ. Ansible Inventory Hosts文件配置

# mkdir /etc/ansible
# touch /etc/ansible/hosts
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.137.6 client
192.168.137.5 server
192.168.137.7 web1
192.168.137.8 web2
# cat /etc/ansible/hosts
[local]
server
client
[web]
web[1:2]
192.168.13.14:52022
jumpter ansible_port=5555 ansible_host=192.168.1.50 ansible_user=xxx ansible_ssh_pass="xxxx"

我这里就添加了两个主机组:local、web,local主机组两台主机,web主机组4台主机。

这里你可以把同一类主机或者是想统一管理的主机放在一个主机组里。

# 其他ansible2.3  inventory参数举例

General for all connections:


ansible_host
The name of the host to connect to, if different from the alias you wish to give to it.
ansible_port
The ssh port number, if not 22
ansible_user
The default ssh user name to use.

Specific to the SSH connection:


ansible_ssh_pass
The ssh password to use (never store this variable in plain text; always use a vault. See Variables and Vaults)
ansible_ssh_private_key_file
Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent.
ansible_ssh_common_args
This setting is always appended to the default command line for sftpscp, and ssh. Useful to configure a ProxyCommand for a certain host (or group).
ansible_sftp_extra_args
This setting is always appended to the default sftp command line.
ansible_scp_extra_args
This setting is always appended to the default scp command line.
ansible_ssh_extra_args
This setting is always appended to the default ssh command line.
ansible_ssh_pipelining
Determines whether or not to use SSH pipelining. This can override the pipelining setting in ansible.cfg.
ansible_ssh_executable (added in version 2.2)
This setting overrides the default behavior to use the system ssh. This can override the ssh_executable setting in ansible.cfg.

Privilege escalation (see Ansible Privilege Escalation for further details):


ansible_become
Equivalent to ansible_sudo or ansible_su, allows to force privilege escalation
ansible_become_method
Allows to set privilege escalation method
ansible_become_user
Equivalent to ansible_sudo_user or ansible_su_user, allows to set the user you become through privilege escalation
ansible_become_pass
Equivalent to ansible_sudo_pass or ansible_su_pass, allows you to set the privilege escalation password (never store this variable in plain text; always use a vault. See Variables and Vaults)
ansible_become_exe
Equivalent to ansible_sudo_exe or ansible_su_exe, allows you to set the executable for the escalation method selected
ansible_become_flags
Equivalent to ansible_sudo_flags or ansible_su_flags, allows you to set the flags passed to the selected escalation method. This can be also set globally in ansible.cfg in the sudo_flags option

Remote host environment parameters:


ansible_shell_type
The shell type of the target system. You should not use this setting unless you have set the ansible_shell_executable to a non-Bourne (sh) compatible shell. By default commands are formatted using sh-style syntax. Setting this to csh or fish will cause commands executed on target systems to follow those shell’s syntax instead.
ansible_python_interpreter
The target host python path. This is useful for systems with more than one Python or not located at /usr/bin/python such as *BSD, or where /usr/bin/python is not a 2.X series Python. We do not use the /usr/bin/env mechanism as that requires the remote user’s path to be set right and also assumes the python executable is named python, where the executable might be named something like python2.6.
ansible_*_interpreter
Works for anything such as ruby or perl and works just like ansible_python_interpreter. This replaces shebang of modules which will run on that host.

New in version 2.1.


ansible_shell_executable
This sets the shell the ansible controller will use on the target machine, overrides executable in ansible.cfg which defaults to /bin/sh. You should really only change it if is not possible to use /bin/sh (i.e. /bin/sh is not installed on the target machine or cannot be run from sudo.).

Examples from an Ansible-INI host file:

Ⅱ. Ansible配置及命令详解

 · module_name

  Ansible将管理功能分成一个个模块,默认是'command'模块,但是command模块不支持shell变量、管道、配额。所以,执行带有管道的命令,可以使用'shell'模块。

· pattern

  如果没有提供'hosts'节点,这是playbook要通信的默认主机组,默认值是对所有主机通信。

  1. 指定一组连续的机器:ansible 192.168.1.* -m ping (指定192.168.1/28网段所有机器)

  2. 指定一组不相关机器:ansible abcd.com:efgh.com -m ping (同样适用于组连接)

  3. 指定在local组,不在web组的机器: local:!web (从左到右依次匹配)

  4. 指定在local组,也在web组的机器:local:&web

-a 指定传入模块的参数

-C -D 一起使用,检查hosts规则文件的修改

-l 限制匹配规则的主机数

--list-hosts 显示所有匹配规则的主机

-m -M指定所使用的模块和模块的路径

--syntax-check 检查语法

-v 显示详细日志

. Ansible命令举例

1> 执行第一条ansible命令

# ansible local -m ping  #使用ping模块
server | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
} client | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
}

由于ansible是基于ssh,这里我们先要配置公钥。

# ssh-keygen -t rsa

# ssh-copy-id -i root@192.168.137.6 #为ansible管理的主机安装server的公钥

再次运行上一条命令:

# ansible local -m ping
server | SUCCESS => {
"changed": false,
"ping": "pong"
} client | SUCCESS => {
"changed": false,
"ping": "pong"
}

2> 其他命令简介

查看local组主机内存使用情况:

# ansible local -a "free -m"
server | SUCCESS | rc= >>
total used free shared buff/cache available
Mem:
Swap:
client | SUCCESS | rc= >>
total used free shared buff/cache available
Mem:
Swap:

若要执行带有管道的命令,可使用shell模块:

# ansible local -m shell -a "df -h | grep /home"
server | SUCCESS | rc= >>
/dev/mapper/cl-home 16G 187M 16G % /home
client | SUCCESS | rc= >>
/dev/mapper/cl-home 16G 187M 16G % /home

限定命令只在一台client主机生效:

# ansible -a "df -h" --limit "client"

执行一个耗时任务:(-B 3600表示最多运行60分钟,-P 60表示每隔60s获取一次状态)

ansible all -B  -P 6 -a "/usr/bin/long_running-operation --do-stuff"

其他ansible参数可使用ansible -h查看。

3> 常用模块命令举例

①file模块

创建文件符链接:

# ansible local -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"

更改文件权限为755,属组为root:root:

ansible local -m file -a "dest=/tmp/resolv.conf mode=755 owner=root group=root"

②service模块

启动NTP服务:

# ansible local -m service -a "name=ntpd state=started enabled=yes"

③copy模块

将本地文件拷贝到远程服务器:

# ansible local -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

更多模块请参考命令ansible-doc -l

模块官网http://docs.ansible.com/ansible/latest/list_of_all_modules.html

Ansible(二) - 配置及命令简介的更多相关文章

  1. Ansible安装配置及命令使用详解

    Ansible和saltstack目前市面上一些其它的项目管理工具有很大的不同,它的设计初衷就是为了更方便.快捷的进行配置管理.它易于安装和使用.语法也非常简单易学.你可以用Ansible将平常复杂的 ...

  2. ansible 三配置和命令集

    一.Ansible配置 Ansible安装好之后的配置文件有如下两种: 1.yum安装,配置文件默认路径为: /etc/ansible/ansible.cfg 2.源码包安装,配置文件路径需要从软件包 ...

  3. Windbg 脚本命令简介 二, Windbg command

    Windbg  脚本命令简介 二, Windbg  script command $<, $><, $$<, $$><, $$>a< (Run Scri ...

  4. 在Mac上配置adb命令

    在Mac上配置adb命令 在Mac OS中使用adb命令时,应进行变量配置,步骤如下: 一.终端中输入 cd ~ 二.输入touch .bash_profile 回车 touch:如果没有,则创建文件 ...

  5. ansible之基本原理及命令

    什么是ansible ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(\(puppet.chef.func.fabric\))的优点,实现了批量系统配置.批量程序部署 ...

  6. ansible的两种命令执行方式 : ad-hoc / playbook以及通过setup模块获取系统参数

    一.什么是ad-hoc和playbook  ad-hoc : 如果我们敲入一些命令去快速的完成一些操作,而不需要将这些执行过的命令刻意保存下来,这样的命令就叫做as-hoc命令,这里不做过多赘述. p ...

  7. (一)ansible 安装配置

    CentOS 7.5 一,安装 yum -y install ansible 二,配置hosts文件 /etc/ansible/hosts s1 ansible_ssh_port= ansible_s ...

  8. Android抓包方法(二)之Tcpdump命令+Wireshark

    Android抓包方法(二) 之Tcpdump命令+Wireshark 前言 做前端测试,基本要求会抓包,会分析请求数据包,查看接口是否调用正确,数据返回是否正确,问题产生是定位根本原因等.学会抓包分 ...

  9. linux网络配置相关命令、虚拟网络接口eth0:0

    网络接口(interface)是网络硬件设备在操作系统中的表示方法,比如网卡在Linux操作系统中用ethX,是由0开始的正整数,比如eth0.eth1...... ethX.而普通猫和ADSL的接口 ...

随机推荐

  1. 帆软的报表系统与泛微OA结合起来,这两个软件麦枫提供了经典的服务。

    一.集成配景泛微OA对企业的代价 泛微协同办公计划能向你供给一个协同的.集成的办公情况,使所有的办公职员都在统一且个性化的信息流派中一路事情, 解脱光阴和地区的限定,实现协同事情与知识治理. Eoco ...

  2. css关键字unset

    今天遇到了一个css属性 display:unset 以为是新增的display的属性值,查了好久,发现并没有这个属性值, 后来发现了unset是css的关键字,将一个属性的属性值设置为unset,目 ...

  3. 设置SQL Server 2005数据库使之可以远程连接

    1. 开启sql2005远程连接功能,开启办法如下: 配置工具->SQL Server外围应用配置器->服务和连接的外围应用配置器->打开MSSQLSERVER节点下的Databas ...

  4. poj3249 拓扑排序+DP

    题意:给出一个有向无环图,每个顶点都有一个权值.求一条从入度为0的顶点到出度为0的顶点的一条路径,路径上所有顶点权值和最大. 思路:因为是无环图,则对于每个点经过的路径求其最大权值有,dp[i]=ma ...

  5. 201521123083《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  6. 【Alpha】——Fifth Scrum Meeting

    一.今日站立式会议照片 二.每个人的工作 成员 昨天已完成的工作 今天计划完成的工作 李永豪 测试统计功能 对统计出现的问题进一步完善 郑靖涛 着手编写报表设计 继续报表设计 杨海亮 协助编写统计功能 ...

  7. 201521123092《java程序设计》第八周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4 ...

  8. 201521123095 《Java程序设计》第1周学习总结

    1. 本周学习总结     开始了对JAVA的初步了解和学习,了解了如何编写简单的JAVA程序.      了解了Java的诞生及发展以及如何运用JVN JRE JDK      JVM让JAVA可以 ...

  9. How To:禁用ubuntu全局菜单(global menu)的方法

    刚从windows转过来的新手可用会觉得ubuntu unity下的全局菜单(global menu)用起来很不方便.下边是介绍去除全局菜单的方法 1.打开终端(可以去dash主页里面搜,也可以直接按 ...

  10. 201521123056 《Java程序设计》第14周学习总结

    1. 本周学习总结 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语句(截图) 参 ...