Ansible 基础配置

  • 主配置文件:/etc/ansible/ansible.cfg
  • ansible配置文件查找顺序
    • 首先检测ANSIBLE_CONFIG变量定义的配置
    • 其次检查当前目录下的./ansible.cfg文件(可以在任意目录创建ansible目录,并参考默认ansible.cfg文件配置ansible.cfg文件)
    • 再次检查当前用户家目录下~/ansible.cfg文件
    • 最后检查/etc/ansible/ansible.cfg文件

Ansible 配置案例

1、在/root目录下创建ansible目录,并参考/etc/ansible/ansible.cfg配置对应的cfg文件

mkdir -p /root/ansible
cd /root/ansible
vim ansible.cfg

2、ansible.cfg 参数解析

[defaults]
inventory = ~/ansible/hosts
#forks = 5
#ask_pass = True
#remote_port = 22
#host_key_checking = False

inventory:主机清单配置文件

forks:ssh并发数量

ask_pass:使用密钥还是密码远程

host_key_checking:是否校验秘钥

3、配置 ~/ansible/hosts

[root@node01 ansible]# pwd
/root/ansible
[root@node01 ansible]# cat hosts
[master]
node01
[agent]
node02
[webserver]
node0[3:4]
[database]
node05
[cluster:children]
webserver
database

[master]:中括号可以设置主机组,组名任意

[cluster:children]:嵌套组,可以在主机组cluster下配置子组(PS:children是关键字)

4、如果没有配置SSH免密可以在hosts文件中进行以下配置

[root@node01 ansible]# cat hosts
[master]
node01
[agent]
node02,node03,node04 [master:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_become_pass=123456

[master:vars]:vars是关键字,给主机组master配置对应的参数

ansible_ssh_user:执行ansible命令时,配置使用的用户是root还是其他用户

ansible_ssh_pass:执行ansible命令时,配置root或其他用户对应的密码

ansible_become_pass:这个参数是在执行ansible命令遇到需要提升权限(例如sudo)时,sudo用户的密码(root用户的密码)

Ansible 使用场景

1、查看所有主机列表

[root@node01 ansible]# ansible all --list-hosts
hosts (4):
node01
node02
node03
node04

2、测试网络连通情况

# 可以根据主机名进行测ping
[root@node01 ansible]# ansible node01 -m ping
node01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
# 也可以根据主机组名进行测ping
[root@node01 ansible]# ansible agent -m ping
node02,node03,node04 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname node02,node03,node04: Name or service not known",
"unreachable": true
}

3、执行shell命令,查看seliux的配置

[root@node01 ansible]# ansible master -m shell -a "cat /etc/selinux/config"
node01 | CHANGED | rc=0 >> # This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

4、通过嵌套主机组对多个主机组进行操作

[root@node01 ansible]# ansible cluster -m ping
node01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node04 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node03 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@node01 ansible]#
[root@node01 ansible]#
[root@node01 ansible]# cat hosts
[master]
node01
[agent]
node02
node03
node04
[master:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_become_pass=123456
[agent:vars]
ansible_ssh_user=root
ansible_ssh_pass=123456
ansible_become_pass=123456
[cluster:children]
master
agent

Ansible - [02] 基础配置以及常用操作场景的更多相关文章

  1. Fedora 28 系统基础配置以及常用软件安装方式

    实验说明: 很多人说Linux很难用,很难上手,其实不然,倘若不玩游戏,其实很多发行版Linux都可以成为主力系统,就比如本章要讲的 Fedora 28.本章会从镜像来源.系统安装.基础配置和常用软件 ...

  2. Hibernate 基础配置及常用功能(三)

    本章重点讲述Hibernate对象的三种状态以及如何配置二级缓存 有关Hibernate的三种状态如何相互转换网上都能查到,官方文档描述的也比较详细.这里主要是针对几个重点方法做代码演示. 一.状态转 ...

  3. Centos 7.2 Jenkins+Ansible+Gitlab 基础配置

    注意:首先准备jenkins服务器  如何搭建jenkins 由于上篇文章中jenkins是采用war并部署在tomcat中来完成的安装,所以这里隆重介绍下启动tomcat的用户:tomcat,下面会 ...

  4. python基础之列表常用操作及知识点小结

    列表(list) List(列表) 是 Python 中使用最频繁的数据类型.列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(所谓嵌套).列表用[ ]标识,是pyth ...

  5. Python基础灬文件常用操作

    文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...

  6. Ansible基础配置与常用模块使用

    环境介绍: Ansible服务端IP:192.168.2.215 Ansible客户端IP:192.168.2.216.192.168.2.218.192.168.2.113   一.创建Ansibl ...

  7. CloudEngine 6800基础配置-02_常用命令操作

    查看未提交配置   system-view ftp server enable display configuration candidate   删除未提交的配置 clear configurati ...

  8. Ansible 快速安装配置,常用模块

    Ansible是一个轻量级的工具,基于python语言实现,通过python中的paramiko来连接并管理机器, 功能强大(YAML,PlayBook,模块化功能),不需要安装客户端, 通过ssh连 ...

  9. Hibernate 基础配置及常用功能(二)

    本章主要是描述几种经典映射关系,顺带比较Hibernate4.x和Hibernate5.x之间的区别. 一.建立测试工程目录 有关实体类之间的相互映射关系,Hibernate官方文档其实描述的非常详细 ...

  10. Hibernate 基础配置及常用功能(一)

    本来是想等全部框架测试完以后再统一发布的,但是随着测试的一点点增加感觉把需要叙述的东西放在一起终将会是一场灾难.所以还是打算分成几章来描述,其中还包括一些有待解决的问题.短期很难腾出时间来仔细阅读Hi ...

随机推荐

  1. Gitlab的备份与恢复,异机转移

    ​注意:异机转移的时候,gitlab的版本必须一致. 一.备份GitLab数据 停止GitLab服务 gitlab-ctl stop unicorn gitlab-ctl stop sidekiq 创 ...

  2. mysql忘记密码的终极解决方案(docker-compose)

    MYSQL8的安全性能有所提高,装好后,各种不适应,需要各种调试. 1. 首先,root密码忘记或是更改,操作步骤: vi mysql/config/my.cnf 在[mysqld]的段中加上一句:s ...

  3. 基于Java实现获取本地IP地址和主机名

    方式一:通过java.net.InetAddress类获取 1 2 3 4 5 6 7 8 public void test1() {  try {   InetAddress addr = Inet ...

  4. 将现有的系统环境文件打包成Docker镜像文件

    一.现有A系统Centos7操作: 备注:A系统里最好不安装Docker,否则会报错 卸载不必要软件包 yum remove -y iwl* *firmware* --exclude=kernel-f ...

  5. Qt/C++音视频开发48-推流到rtsp服务器

    一.前言 之前已经打通了rtmp的推流,理论上按照同样的代码,只要将rtmp推流地址换成rtsp推流地址,然后格式将flv换成rtsp就行,无奈直接遇到协议不支持的错误提示,网上说要换成rtp,换了也 ...

  6. [转]vue调试工具vue-devtools安装及使用(亲测有效,望采纳)

    vue调试工具vue-devtools安装及使用(亲测有效,望采纳) 本文主要介绍 vue的调试工具 vue-devtools 的安装和使用 工欲善其事, 必先利其器, 快快一起来用vue-devto ...

  7. springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.

    错误描述: springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController r ...

  8. pdf文件内容分析工具简介(研究pdf标准好帮手)

    前言:PDF文件标准诞生于20年前,从今天的视角来看,标准设计理念有些过时了:然而,pdf文件生态已经形成,尾大不掉吧.纵然有各种缺点,但是不可否认的事实:pdf是当今应用最广泛的版式文件. pdf文 ...

  9. MySQL-扩展

    1.行转列 源数据: 目标数据: 数据准备 -- 建表插入数据 drop table if exists time_temp; create table if not exists time_temp ...

  10. thewall靶机

    includes.php 内有文件读取漏洞 一开始是想着直接用为协议写入一句话木马但是后来发现不行 因为他的文件读取方式长这样 点击查看代码 <?php include ('/var/www/h ...