文章目录

  • 自定义模块
  • 一、setup模块采集系统信息
  • 1.格式
  • 2.过滤内存
  • 3.过滤环扣
  • 二、变量引用json数据方式
  • 1.将命令结果注册成变量,并用debug输出或直接在template或者playbook中引用
  • 2.引用json字典数据
  • 3.引用json数组数据
  • 4.引用facts数据
  • 三、注册和定义变量方式
  • 1.命令行传递
  • 2.playbook中vars
  • 3.register注册
  • 4.set_fact定义
  • 5.var_file定义
  • 6.inventory中主机变量和主机组变量
  • 四、两种自定义数据采集方式
  • 1.通过facts文件
  • 2.通过python脚本
  • 五、自定义模块
  • 1.查看模块
  • 2.编写模块(/usr/share/my_modules)
  • 3.文件内容
  • 4.启动模块目录
  • 5.测试自定义模块


自定义模块

一、setup模块采集系统信息

facts组件是用来收集被管理节点信息的,使用setup模块可以获取这些信息,facts收集的信息是json格式的,其内任一项都可以当作变量被直接引用(如在playbook、jinja2模版中)引用

1.格式
ansible IP -m setup

过滤

[root@ansinble_center tmp]#ansible 192.168.100.64 -m setup -a "filter=changed"

filter过滤指定数据项

ansible mount模块 ansible tar模块_json

2.过滤内存
[root@ansinble_center roles]#ansible 192.168.217.131 -m setup -a "filter=ansible_*_mb"

ansible mount模块 ansible tar模块_json_02

3.过滤环扣
[root@ansinble_center roles]#ansible 192.168.217.131 -m setup -a "filter=ansible_interfaces"

ansible mount模块 ansible tar模块_自定义_03

二、变量引用json数据方式

1.将命令结果注册成变量,并用debug输出或直接在template或者playbook中引用

vim debug.yaml

---
- hosts: 192.168.217.131
  tasks:
    - shell: echo hello world
      register: say_hi
    - debug: var=say_hi

结果输出

ansible mount模块 ansible tar模块_数据_04

2.引用json字典数据

通过key[‘dict’]或者key.dict

---
- hosts: 192.168.217.131
  tasks:
    - shell: echo hello world
      register: say_hi
    - debug: var=say_hi.stdout
    - debug: var=say_hi['stdout']

输出结果

ansible mount模块 ansible tar模块_json_05

"key.dict"或"key[‘dict’]“的方式都能引用,但在dict字符串本身就包含”."的时候,应该使用中括号的方式引用。例如:anykey[‘192.168.217.131’]

3.引用json数组数据

通过key[N],N从0开始

---
- hosts: 192.168.217.131
  tasks:
    - shell: echo hello world
      register: say_hi
    - debug: var=say_hi.stdout_lines[0]

输出结果

ansible mount模块 ansible tar模块_数据_06

4.引用facts数据
---
- hosts: 192.168.217.131
  tasks:
    - name: setup
      setup: filter='ansible_eth0'
      register: 
    - debug: var=ansible_eth0.ipv4.address

还可以改成这样

---
- hosts: 192.168.217.131
  tasks:
  	- debug: var=ansible_eth0.ipv4.address

输出结果

ansible mount模块 ansible tar模块_json_07

三、注册和定义变量方式

1.命令行传递
ansible localhost -m shell -a "echo {{say_hi}}" -e 'say_hi="hello world"'
localhost | CHANGED | rc=0 >>
hello world
2.playbook中vars
---
- hosts: localhost
  vars:
    var1: value1
	var2: value2
  tasks:
    - debug: msg="{{var1}} {{var2}}"
      vars:
        var2: value2.2

测试结果

ansible mount模块 ansible tar模块_linux_08

3.register注册
---
- hosts: localhost
  tasks:
	- shell: echo haha
      register: say_hi
    - debug: var=say_hi.stdout

测试结果

ansible mount模块 ansible tar模块_数据_09

4.set_fact定义
---
- hosts: 192.168.217.131
  tasks:
    - shell: echo "hello world"
      register: say_hi
    - debug: var=say_hi
    - debug: var=say_hi.stdout
    - debug: var=say_hi['stdout']
    - set_fact: one_fact= "something"
    - template: src=test.txt dest=/tmp/test.txt

text.txt文件内容

The result of command is: {{ say_hi.stdout }}
The result of command is: {{ say_hi.stdout_lines[0] }}
The Address of eht0 is: {{ ansible_eth0.ipv4.address }}
The Address of eht0 is: {{ ansible_eth0.ipv4 }}

测试结果

ansible mount模块 ansible tar模块_ansible mount模块_10

去客户端查看文件内容

ansible mount模块 ansible tar模块_linux_11

5.var_file定义
---
- hosts: localhost
  vars_files:
    - /tmp/var_file1.yml
   tasks:
    - debug: msg="{{var1}} {{var2}}"

测试结果

ansible mount模块 ansible tar模块_linux_12

6.inventory中主机变量和主机组变量
192.168.100.65 ansible_ssh_port=22 var1=1
[centos7]
192.168.100.63
192.168.100.64
192.168.100.65 var1=2
[centos7:vars]
var1=2.2
var2=3
[all:vars]
var2=4

测试结果

[root@ansinble_center tmp]#ansible 192.168.217.131 -i inventory.yaml -m shell -a 'echo "{{var1}} {{var2}}"'

ansible mount模块 ansible tar模块_数据_13

  • 主机变量优先级高于主机组变量,给定的主机组变量优先级高于all特殊组。

四、两种自定义数据采集方式

1.通过facts文件
  • 在客户端
[root@localhost facts.d]# mkdir /etc/ansible/facts.d
[root@localhost facts.d]# cd /etc/ansible/facts.d

创建cpuinfo.fact文件

vim cpuinfo.fact

[cpu]
cpunum=4
cpuname=i7 7700k
  • 在服务端测试
[root@ansinble_center tmp]#ansible 192.168.217.131 -m setup -a "filter='ansible_local'"

测试结果

ansible mount模块 ansible tar模块_linux_14

注意:这是在客户端创建操作,在服务端测试的

2.通过python脚本

写python脚本获取最大支持的进程数

#!/usr/bin/env 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()

写入facts.d/get_process.fact,给执行权限

[root@localhost facts.d]#chmod +x get_process.fact

测试数据

[root@localhost facts.d]#ansible 192.168.217.131 -m setup -a "filter='ansible_local'"

测试结果

ansible mount模块 ansible tar模块_linux_15

在模版文件中调用变量测试

test.yaml

- hosts: 192.168.217.131
  remote_user: root
  tasks:
    - name: copy conf file nginx.conf
      template: src=/tmp/nginx1.conf dest=/tmp/nginx2.conf

/tmp/nginx1.conf文件内容

events{
	worker_connections {{ ansible_local.get_process.pnum }};
}

测试

[root@ansinble_center tmp]#ansible-playbook /tmp/test.yaml

测试结果

ansible mount模块 ansible tar模块_linux_16

客户端查看

[root@localhost facts.d]# cat /tmp/nginx2.conf

ansible mount模块 ansible tar模块_数据_17

五、自定义模块

/usr/share/my_modules目录

1.查看模块
[root@ansinble_center tmp]#ansible-doc -l
2.编写模块(/usr/share/my_modules)
[root@ansinble_center tmp]#mkdir /usr/share/my_modules
[root@ansinble_center tmp]#vim uptime
3.文件内容
#!/usr/usr/python

import json
import os

up = os.popen('uptime').read()
dic = {"result":up}
print json.dumps(dic)
4.启动模块目录
[root@ansinble_center tmp]#vim /etc/ansible/ansible.cfg
把这行的注释去掉library = /usr/share/my_modules/
5.测试自定义模块
[root@ansinble_center tmp]#ansible all -m uptime

ansible mount模块 ansible tar模块_json_18