Ansible 常见模块

Install

# CentOS Linux release 7.6.1810 (Core)
# ansible.noarch 0:2.9.27-1.el7
yum install -y ansible

# Config
cat /etc/ansible/ansible.cfg | grep -v '^#' | grep -v '^$'
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]

#
┌─[09:33:33 root@k8s1]─[/etc/ansible]
└──╼ # ll
total 24
-rw-r--r-- 1 root root 19985 Jan 16  2022 ansible.cfg
-rw-r--r-- 1 root root  1016 Jan 16  2022 hosts
drwxr-xr-x 2 root root     6 Jan 16  2022 roles

# 
vim /etc/ansible/hosts
[k8s] #定义一个k8s组,这个组下有5个服务器,默认做了免密登陆
10.30.17.161 
10.30.17.162
10.30.17.163
10.30.17.164
10.30.17.165

[k8s-no] #定义一个lb组,这个lb组的服务器如果没有做免密登陆,所以需要写上用户名和密码
10.30.17.161 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=""
10.30.17.162 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=""
10.30.17.163 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=""
10.30.17.164 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=""
10.30.17.165 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=""

或者
[lb]
192.168.118.138	
192.168.118.139 
[lb:vars]  #vars是一个变量,前面lb是组名,表示lb组使用下面这些变量,这样就能实现lb组使用相同的用户名密码
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass="admin123"

#
ansible -i hosts k8s -m ping
10.30.17.165 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.30.17.164 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.30.17.162 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.30.17.161 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
10.30.17.163 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

Setup

Facts信息	                  含义
ansible_all_ipv4_addresses	所有目标主机的 IPv4 地址列表。
ansible_all_ipv6_addresses	所有目标主机的 IPv6 地址列表。
ansible_date_time	系统时间。
ansible_kernel	内核版本。
ansible_default_ipv4	默认网关的 IPv4 地址。
ansible_default_ipv6	默认网关的 IPv6 地址。
ansible_distribution	Linux 系统发行版本,例如 CentOS、Ubuntu、等。
ansible_nodename	主机名。
ansible_pkg_mgr	包管理器,例如 yum、apt、dpkg 等。
ansible_python_version	Python 版本。
ansible_processor_cores	CPU核数。
ansible_processor_count	逻辑 CPU 核心数,包括超线程。
ansible_cpu_info	CPU 信息字典,包含 CPU 频率、架构等信息。
ansible_mem_total	总内存容量。
ansible_mem_free	可用内存容量。
ansible_mem_used	已使用内存容量。
- name: 收集特定Facts信息
  hosts: all
  tasks:
    - name: 收集硬件信息
      setup:
        gather_subset: hardware
    - name: 收集网络信息
      setup:
        gather_subset: network
    - name: 收集Fact信息
      setup:
        gather_subset: fact
    - name: 收集配置信息
      setup:
        gather_subset: config
    - name: 不收集任何Facts信息
      setup:

其他模块

template
setup
shell
command
systemd
file
blockinfile
uri
until
block
	local_action
pam_limits
replace

 

posted @ 2025-03-27 09:17  しみずよしだ  阅读(26)  评论(0)    收藏  举报