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