ansible之自定義模塊

一、註冊和定義變量方式

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、編寫模塊

  1. mkdir= /usr/share/my_modules/ (創建路徑)

  2. touch /usr/share/my_modules/uptime (創建模塊)

  3. 編寫 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"
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章