ansible常用方法

1.安装ansible

yum -y install ansible

2.主机清单推荐格式

[root@controller ~]# vi /etc/ansible/hosts 
[controllers]
# ansible_ssh_port=22
controller ansible_ssh_host=192.168.100.10

[computes]
compute ansible_ssh_host=192.168.100.20

3.常用命令

# 1、ping全部主机
ansible all -m ping

# 2、查看compute主机的全部信息
ansible compute -m setup

# 有过滤的作用
ansible compute -m setup -a "fileter='ansible_fqdn'"

# 模糊匹配
ansible compute -m setup -a 'filter=*eth0*'

# 3、playbook获取指定变量的方法
[root@controller ~/ansible]# vim get_info.xml                           
- hosts: compute

  tasks:
    - name: create file named ip address
      shell: "echo {{ ansible_eth0['ipv4']['address'] }} > ~/a.txt"

执行之后
[root@compute ~]# cat a.txt 
192.168.100.20

4.Playbook的解耦和

1、初始化新的Playbook

# 初始化一个名为install_zabbix剧本
[root@controller ~]# ansible-galaxy init install_zabbix
- Role install_zabbix was created successfully

# 各个目录的解释
[root@controller ~]# tree install_zabbix/
install_zabbix/         
├── defaults              # 低优先级变量
│   └── main.yml
├── files                 # 存放文件,没有变量的配置文件,使用copy推送的时候使用,shell、txt
├── handlers              # 触发器文件
│   └── main.yml
├── meta                  # 依赖关系文件
│   └── main.yml
├── README.md
├── tasks                 # 工作任务文件
│   └── main.yml
├── templates             # jinja2模板文件,里面有变量,使用temlate推送的文件都可以放到里面。
├── tests                 # 测试文件 -C 的时候
│   ├── inventory
│   └── test.yml
└── vars                  # 变量文件,优先级比较高的
    └── main.yml

8 directories, 8 files

# 保证site.yml和要执行的任务在同一个目录就好了。这里移动了一下。
[root@controller ~/ansible_playbook]# cat site.yml 
# 模板
#- hosts: all            <====要运行的主机
#  roles:
#      示要执行install_zabbix的这个任务,在m01这个机器上面
#    - { role: install_zabbix , when: ansible_fqdn == "m01" }

- hosts: all
  roles:
    - { role: install_zabbix }
    

[root@controller ~/ansible_playbook]# ls
install_zabbix  site.yml

[root@controller ~/ansible_playbook]# cat install_zabbix/tasks/main.yml 
- name: Send config file zabbix Agent
  template:
    src: zabbix_agentd.conf
    dest: /etc/zabbix/

# 模版文件夹里面的模板文件
[root@controller ~/ansible_playbook]# cat install_zabbix/templates/zabbix_agentd.conf 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0

Server={{ ansible_eth0['ipv4']['address'] }}
ServerActive={{ ansible_eth0['ipv4']['address'] }}
Hostname={{ ansible_fqdn }}

Include=/etc/zabbix/zabbix_agentd.d/*.conf

# 执行这个文件
[root@controller ~/ansible_playbook]# ansible-playbook site.yml

# 显示的结果
[root@compute ~]# cat /etc/zabbix/zabbix_agentd.conf 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0

Server=192.168.100.20
ServerActive=192.168.100.20
Hostname=compute

Include=/etc/zabbix/zabbix_agentd.d/*.conf
posted @   Gshelldon  阅读(136)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示