ansible清单文件的配置方法、配置文件的配置、临时命令的用法

ansible清单文件的配置方法、配置文件的配置、临时命令的用法

一、构建ansible清单

1、定义清单

清单定义Ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。

定义清单的两种方式:

  • 静态主机清单:文本文件定义
  • 动态主机清单:根据需要使用外部信息提供程序通过脚本或其他程序来生成

2、使用静态清单指定受管主机

每一部分的开头为以中括号括起来的主机组名称。其后为该组中每一受管主机的主机名或IP地址,每行一个。

[root@localhost ~]# vim /etc/ansible/hosts
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110

# If you have multiple hosts following a pattern you can specify
# them like this:

## www[001:006].example.com

# Ex 3: A collection of database servers in the 'dbservers' group

## [dbservers]
## 
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57

3、验证清单

[root@localhost ~]# vim /etc/ansible/hosts
green.example.com
blue.example.com
192.168.100.1
192.168.100.10		//取消此四行的注释

//验证green主机是否存在于清单
[root@localhost ~]# ansible green.example.com --list-hosts
  hosts (1):
    green.example.com
    
//列出清单中的所有主机
[root@localhost ~]# ansible all --list-hosts
  hosts (4):
    green.example.com
    blue.example.com
    192.168.100.1
    192.168.100.10

[root@localhost ~]# vim /etc/ansible/hosts
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110		//取消此主机组的注释

//列出指定主机组
[root@localhost ~]# ansible webservers --list-hosts
  hosts (4):
    alpha.example.org
    beta.example.org
    192.168.1.100
    192.168.1.110

如果清单中含有名称相同的主机和主机组,ansible 命令将显示警告并以主机作为其目标。主机组则被忽略。

[root@localhost ~]# vim /etc/ansible/hosts
[webservers]
webservers		//添加一个和主机组相同名称的主机
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110


//会列出主机并警告
[root@localhost ~]# ansible webservers --list-hosts
[WARNING]: Found both group and host with same name: webservers
  hosts (1):
    webservers

4、覆盖清单文件的位置

/etc/ansible/hosts文件被视为系统的默认静态清单文件。不过,通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。

//修改默认清单文件位置
[root@localhost ~]# cd /etc/ansible/
[root@localhost ansible]# touch inventory
[root@localhost ansible]# vim ansible.cfg 
inventory      = /etc/ansible/inventory		//取消注释并修改指定位置

5、构建清单

//写入内容
[root@localhost ansible]# vim inventory 
192.168.183.137

[webservers]
192.168.183.135

192.168.183.136

//列出默认清单文件中的所有受管主机
[root@localhost ansible]# ansible all  --list-hosts
  hosts (3):
    192.168.183.137
    192.168.183.135
    192.168.183.136
    
//列出不属于任何主机组的受管主机
[root@localhost ansible]# ansible ungrouped --list-hosts
  hosts (1):
    192.168.183.137
    
//列出属于某组的受管主机
[root@localhost ansible]# ansible webservers --list-hosts
  hosts (2):
    192.168.183.135
    192.168.183.136

二、ansible配置文件

Ansible配置文件中的一些常用参数

[root@localhost ~]# vim /etc/ansible/ansible.cfg
······
[defaults]

# some basic default values...

inventory      = /etc/ansible/inventory
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
#poll_interval  = 15
#sudo_user      = root
#ask_sudo_pass = True
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False

······
参数 作用
inventory 定义了Ansible默认的主机配置文件,默认为/etc/ansible/hosts
library 定义了Ansible默认搜寻模块的位置,默认为/etc/ansible/my_modules/目录
remote_tmp 定义了Ansible远程执行临时文件
pattern 定义了Ansible通信的主机,该参数默认为*,表示与所有主机进行通信
forks 定义了Ansible的并行进程数,默认为5
poll_interval 定义了回频率或轮询间隔时间
sudo_user 定义了sudo远程执行用户名
ask_sudo_pass 定义了使用sudo是否需要输入密码
ask_pass 定义了是否需要输入密码
transport 定义了Ansible的通信机制
remote_port 定义了Ansible的通信端口,默认为22
module_lang 定义了Ansible模块和系统之间通信的语言
gathering 控制facts信息的收集
roles_path 用于搜索Ansible中的roles
host_key_checking 用于检查主机密钥
sudo_exe 用于指定sudo远程执行命令
sudo_flags 用于传递sudo以外的参数
timeout 用于设置SSH超时时间
remote_user 用于设置远程登录用户名
log_path 用于指定Ansible日志文件,默认情况下为/var/log/ansible.log
module_name 用于指定Ansible在默认情况下的执行模块,默认为command
executable 用于指定Ansible执行的shell环境
hash_behavior 用于指定特定的优先级覆盖变量
jinjia2_extensions 设置允许开启jinjia2拓展模块
private_key_file 用于指定私钥文件存储位置
display_skipped_hosts 用于显示任何跳过任务的状态
system_warnings 用于禁用系统显示ansible潜在问题警告
deprecation_warnings playbook输出禁用“不建议使用”警告
command_warnings command模块Ansible默认发出警告
pipelining 用于开启pipe SSH通道优化

三、临时命令的用法

一种最简单的临时命令使用ping模块。此模块不执行ICMP ping,而是检查能否在受管主机上运行基于Python的模块。例如,以下临时命令确定清单中的所有受管主机能否运行标准的模块:


[root@localhost ~]# vim /etc/ansible/inventory 
web1 ansible_user=root ansible_password=runtime
[root@localhost ~]# vim /etc/hosts
192.168.183.140 web1
[root@localhost ~]# ssh web1
[root@localhost ~]# ansible web1 -m ping
web1 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Ansible常用模块

模块类别 模块
文件模块 copy:将本地文件复制到受管主机 file:设置文件的权限和其他属性 lineinfile:确保特定行是否在文件中 synchronize:使用rsync同步内容
软件包模块 package:使用操作系统本机的自动检测软件包管理器管理软件包 yum:使用yum管理软件包 apt:使用APT管理软件包 dnf:使用dnf管理软件包 gem:管理Ruby gem pip:从PyPI管理Python软件包
系统模块 firewalld:使用firewalld管理防火墙 reboot:重启计算机 service:管理服务 user:添加、删除和管理用户帐户
Net Tools模块 get_url:通过HTTP、HTTPS或FTP下载文件 nmcli:管理网络 uri:与Web服务交互

临时命令使用user模块来确保runtime用户存在于web1上并且其UID为4000:

[root@localhost ~]# ansible web1 -m user -a 'name=runtime uid=4000 state=present'
web1 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 4000,
    "home": "/home/runtime",
    "name": "runtime",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 4000
}
[root@localhost ~]# ansible all -a 'id runtime'
web1 | CHANGED | rc=0 >>
uid=4000(runtime) gid=4000(runtime) groups=4000(runtime)

猜你喜欢

转载自blog.csdn.net/qq_65998623/article/details/127429265