ansible:vars

本文介绍了Ansible中如何使用变量,包括vars属性方法和vars_files,以及如何获取主机的自定义信息,特别是当这些信息不在默认的/etc/ansible/facts.d目录下时,如何指定目录进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. ansible的变量

在这里插入图片描述
在这里插入图片描述

[root@server4 ~]# cat vars.yml 
---
- hosts: testB
  vars:
    testvar1: testfile    # 或者- testvar1: testfile
  remote_user: root
  tasks:
  - name: task1
    file:
      path: /testdir/{{ testvar1 }}
      state: touch

# 测试:
[root@server4 ~]# ansible-playbook vars.yml
[root@server3 testdir]# ls
testfile

在这里插入图片描述

[root@server4 ~]# cat vars.yml 
---
- hosts: testB
  vars:
    - testvar1: testfile
    - testvar2: testdirectory
  remote_user: root
  tasks:
  - name: task1
    file:
      path: /testdir/{{ testvar1 }}
      state: touch
  - name: task2
    file:
      path: /testdir/{{ testvar2 }}
      state: directory

vars属性方法
在这里插入图片描述

[root@server4 ~]# cat vars2.yml 
---
- hosts: testB
  remote_user: root
  vars:
    httpd:
      conf80: /etc/httpd/conf.d/80.conf
      conf8080: /etc/httpd/conf.d/8080.conf

  tasks:
  - name: task1
    file:
      path: "{{httpd.conf80}}"
      state: touch
  - name: task2
    file:
      path: "{{httpd.conf8080}}"   # path={{httpd.conf8080}}
      state: touch

[root@server4 ~]# cat vars2.yml 
---
- hosts: testB
  remote_user: root
  vars:
    httpd:
      conf80: /etc/httpd/conf.d/80.conf
      conf8080: /etc/httpd/conf.d/8080.conf

  tasks:
  - name: task1
    file:
      path: "{{httpd.conf80}}"
      state: touch
  - name: task2
    file:
      path: "{{httpd['conf8080']}}"
      state: touch

2. vars_files

在这里插入图片描述
在这里插入图片描述

[root@server4 ~]# cat vars3.yml 
---
- hosts: testB
  remote_user: root
  vars_files:
    - httpd_vars.yml
  tasks:
  - name: task1
    file:
      path: "{{httpd.conf80}}"
      state: touch
  - name: task2
    file:
      path: "{{httpd['conf8080']}}"
      state: touch

2. 获取信息

[root@server4 ~]# ansible testB -m setup
[root@server4 ~]# ansible testB -m setup -a "filter=*mb*"  # 过滤出想要的内容

得到主机自定义信息:

[root@server3 conf.d]# mkdir /etc/ansible
[root@server3 conf.d]# cd /etc/ansible/
[root@server3 ansible]# mkdir facts.d
[root@server3 ansible]# ls
facts.d
[root@server3 ansible]# cd facts.d/
[root@server3 facts.d]# cat info.fact 
[message]
msg1=first message
msg2=second message

# 测试:
[root@server4 ~]# ansible testB -m setup -a "filter=ansible_local"
server3 | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "info": {
                "message": {
                    "msg1": "first message", 
                    "msg2": "second message"
                }
            }
        }, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

如果info.fact不再/etc/ansible/facts.d中,需要指定目录

[root@server4 ~]# ansible testB -m setup -a "fact_path=/testdir
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值