文章目录
一、注册和定义变量方式
1、命令行传递
在-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
2、在playbook中vars中定义
[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的值为在当前任务下定义的那个值。这里的作用域和编程中局部变量和全局变量的原理是一样的
3、register注册
[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
4、set_fact定义
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
5、var_files定义
[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
6、inventory中主机变量和主机组变量
一般编辑/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
二、变量引用json数据方式
1、引用json字典数据
通过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
2、引用json数组数据
通过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
3、引用facts数据
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
三、自定义数据采集方式
1、通过.fact文件
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 }
2、通过python脚本
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 }
四、自定义模块
1、编写模块
-
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)
2、启用模块目录
vim /etc/ansible/ansible.cfg
library=/usr/share/my_modules (自定义模块存放的路径)
3、测试自定义模块
[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 (自定义模块存放的路径)
3、测试自定义模块
[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"
}