ansible字符串處理(一)

[root@node-1 test]# ansible-playbook hba_card_check.yml 

PLAY [compute[0]] *******************************************************************************************************
Tuesday 12 March 2019  08:50:44 +0800 (0:00:00.097)       0:00:00.097 ********* 

TASK [get hba hosts] ************************************************************************************************************
ok: [172.23.2.9]
Tuesday 12 March 2019  08:50:45 +0800 (0:00:00.425)       0:00:00.522 ********* 

TASK [set_fact] *******************************************************************************************************************
ok: [172.23.2.9]
Tuesday 12 March 2019  08:50:45 +0800 (0:00:00.112)       0:00:00.635 ********* 

TASK [debug] *********************************************************************************************************************
ok: [172.23.2.9] => {
    "hba_hosts.stdout_lines": [
        "/sys/class/fc_host/host11", 
        "/sys/class/fc_host/host12"
    ]
}
Tuesday 12 March 2019  08:50:45 +0800 (0:00:00.120)       0:00:00.756 ********* 

TASK [get hba card state] *********************************************************************************************************
changed: [172.23.2.9]
Tuesday 12 March 2019  08:50:45 +0800 (0:00:00.307)       0:00:01.064 ********* 

TASK [debug] ***********************************************************************************************************************
ok: [172.23.2.9] => {
    "port_state.stdout_lines": [
        "[u'', u'sys', u'class', u'fc_host', u'host11'] Online", 
        "[u'', u'sys', u'class', u'fc_host', u'host12'] Online"
    ]
}

PLAY RECAP ************************************************************************************************************************
172.23.2.9                 : ok=5    changed=1    unreachable=0    failed=0

上邊是完整的輸出。
代碼以下:node

---
- hosts: compute[0]
  gather_facts: false
  tasks:
  - name: get hba hosts
    shell: ls -d /sys/class/fc_host/host*
    register: hba_hosts
    failed_when: false
    changed_when: false
  - set_fact:
        hba_count: "{{hba_hosts.stdout_lines|length}}"
    when: hba_hosts.rc == 0
  - name: get hba card state
    shell: |-
        {%- for host in hba_hosts.stdout_lines -%}
            echo -n "{{ host.split('/') }} "
            cat {{ host }}/port_state;
        {%- endfor -%}
    register: port_state
  - debug: var=port_state.stdout_lines

那麼字符傳的輸出處理在get hba cart state這個task的shell裏, 使用了jinja2循環,在echo時,對元素host進行路徑字符串(hba_hosts.stdout_lines的元素)分割,想獲得路徑最右的‘/’後的文件夾名稱,也就是shell的basename,然而在playbook中,jinja2獲取的變量是utf-8編碼,而python2.7和shell都是ascii編碼,因此此時要獲得沒有u‘’的字符串須要對這個元素host進行編碼,使用.encode()方法(python2默認編碼ascii),則能夠去掉u'',獲得咱們想要的結果.
echo -n "{{ host.encode().split('/') }} "
輸出即爲以下:python

TASK [debug] *******************************************************************************************************************
ok: [172.23.2.9] => {
    "port_state.stdout_lines": [
        "['', 'sys', 'class', 'fc_host', 'host11'] Online", 
        "['', 'sys', 'class', 'fc_host', 'host12'] Online"
    ]
}

然而我要獲取hosts11 Online和host12 Online的列表,則需使用jinja2的last過濾器獲取該值
echo -n "{{ host.encode().split('/') }}|last "
輸出以下:shell

ok: [172.23.2.9] => {
    "port_state.stdout_lines": [
        "host11 Online", 
        "host12 Online"
    ]
}

然而,在大規模的環境裏,系統信息的獲取時,我的覺得shell效率要高於python的,故此處有shell方法:python2.7

{%- for host in hba_hosts.stdout_lines -%}
            host={{host}}
            echo -n "${host##*/} "                                    # echo -n "$(basename $host)  "
            #echo -n "{{ host.encode().split('/')|last}} "
            cat {{ host }}/port_state;
{%- endfor -%}
相關文章
相關標籤/搜索