在-e后接参数名和参数值
[root@ansible_center tmp]# ansible test -m shell -a "echo {{ say_hi }}" -e 'say_hi="hello world"'
192.168.189.134 | CHANGED | rc=0 >>
hello world
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
vars:
var1: value1
var2: value2
tasks:
- debug: msg="{{ var1 }}{{ var2 }}"
vars:
var2: value2.2
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"msg": "value1value2.2"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注意:debug这个任务调用var2的时候,var2的值为在当前任务下定义的那个值。这里的作用域和编程中局部变量和全局变量的原理是一样的
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
tasks:
- shell: echo haha
register: say_hi
- debug: var=say_hi.stdout
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [shell] *******************************************************************
changed: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"say_hi.stdout": "haha"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
解释:这里的register把模块执行后的结果赋值给say_hi
set_fact
模块可以自定义facts,这些自定义的facts可以通过template或者变量的方式在playbook中使用。
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
tasks:
- set_fact: one_fact="something"
- debug: var=one_fact
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [set_fact] ****************************************************************
ok: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"one_fact": "something"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
vars_files:
- /tmp/var_file1.yml
tasks:
- debug: msg="{{ var1 }}{{ var2 }}"
[root@ansible_center tmp]# cat var_file1.yml
var1: hello
var2: value2
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"msg": "hellovalue2"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
一般编辑/etc/ansible/hosts文件,有如下定义方式
#对特定主机设置变量
192.168.100.65 ansible_shh_port=22
#对主机组设置变量
[centos7]
192.168.100.62
192.168.100.63
192.168.100.64
[centos7:vars]
var1=2.2
var=3
#设置对所有主机有效的全局变量
[all:vars]
var2=4
通过key[‘dict’]或者key.dict获取
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
tasks:
- shell: echo hello world
register: say_hi
- debug: var=say_hi.stdout
- debug: var=say_hi['stdout']
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [shell] *******************************************************************
changed: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"say_hi.stdout": "hello world"
}
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"say_hi['stdout']": "hello world"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
通过key[N],N从0开始
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
tasks:
- shell: echo -e "hello\n world"
register: say_hi
- debug: var=say_hi.stdout_lines[0]
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [shell] *******************************************************************
changed: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"say_hi.stdout_lines[0]": "hello"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
playbook执行前会自动收集facts数据,所以我们可以在playbook中直接使用他们
下例为引用被管理端的ipv4地址
[root@ansible_center tmp]# cat test.yaml
---
- hosts: 192.168.189.134
tasks:
- debug: var=ansible_ens33.ipv4.address
[root@ansible_center tmp]# ansible-playbook test.yaml
PLAY [192.168.189.134] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.189.134]
TASK [debug] *******************************************************************
ok: [192.168.189.134] => {
"ansible_ens33.ipv4.address": "192.168.189.134"
}
PLAY RECAP *********************************************************************
192.168.189.134 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible除了能获取到预定义的fact的内容,还支持手动为某个主机定制fact。称之为本地fact。本地fact默认存放于被控端的/etc/ansible/facts.d
目录下,如果文件为ini
格式或者json
格式,ansible会自动识别。以这种形式加载的fact是key为ansible_local
的特殊变量。
例子
在被控端新建.fact文件
[root@ansible_node1 ~]# mkdir -p /etc/ansible/facts.d
[root@ansible_node1 ~]# cd /etc/ansible/facts.d
[root@ansible_node1 facts.d]# vim cpuinfo.fact
[root@ansible_node1 facts.d]# cat cpuinfo.fact
[cpu]
cpunum=4
cpuname=i5xxxx
在center做测试
[root@ansible_center tmp]# ansible test -m setup -a "filter=ansible_local"
192.168.189.134 | SUCCESS => {
"ansible_facts": {
"ansible_local": {
"cpuinfo": {
"cpu": {
"cpuname": "i5xxxx",
"cpunum": "4"
}
}
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
python脚本同样放在放于被控端的/etc/ansible/facts.d
目录下,并且要赋予它执行权限
例子:获取最大支持进程数的python脚本
脚本内容
[root@ansible_node1 facts.d]# cat get_process.fact
#!/usr/bin/python
import os,json
def get_process_num():
dic = {}
file = os.popen('ulimit -n').read().strip()
dic['pnum']=file
print json.dumps(dic)
if __name__=="__main__":
get_process_num()
[root@ansible_node1 facts.d]# chmod +x get_process.fact
测试
[root@ansible_center tmp]# ansible test -m setup -a "filter=ansible_local"
192.168.189.134 | SUCCESS => {
"ansible_facts": {
"ansible_local": {
"cpuinfo": {
"cpu": {
"cpuname": "i5xxxx",
"cpunum": "4"
}
},
"get_process": {
"pnum": "1024"
}
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
mkdir= /usr/share/my_modules/ (创建路径)
touch /usr/share/my_modules/uptime (创建模块)
编写 uptime
#!/usr/bin/python
import json,os
up = os.popen("uptime").read()
dic={"result":up}
print json.dumps(dic)
vim /etc/ansible/ansible.cfg
library=/usr/share/my_modules (自定义模块存放的路径)
[root@ansible_center my_modules]# ansible test -m uptime
192.168.189.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"result": " 00:18:11 up 13:36, 2 users, load average: 0.16, 0.05, 0.06\n"
}
library=/usr/share/my_modules (自定义模块存放的路径)
[root@ansible_center my_modules]# ansible test -m uptime
192.168.189.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"result": " 00:18:11 up 13:36, 2 users, load average: 0.16, 0.05, 0.06\n"
}