Ansible

在 Ansible 中建構 JSON 對象列表

  • May 13, 2020

我正在嘗試在 Ansible 中建構 JSON 對象的數組/列表。我的劇本是通過指定來呼叫的--extra-vars "userids=123456,654321"

然後我嘗試使用以下內容建構列表。“待定”只是一個佔位符。我將在後面的劇本中使用從 SQL 查詢中檢索到的數據填充這些值。

- name: Initiate User List
     set_fact:
       all_users: []    

   - name: Add new JSON Objects to List
     set_fact:
       all_users: "{{ all_users+ [{ 'UserID': '{{ item }}', 'UserType': 'TBD', 'UserName': 'TBD' }] }}"
     loop: "{{ userids.split(',') }}"

   - name: Debug
     debug:
       var: all_users

列表排序的建構,但它跳過了列表中的第一項。這是結果輸出:

TASK [Initiate User List] *************************************************************************************************************************************************************
ok: [localhost]

TASK [Add new JSON Objects to List] ******************************************************************************************************************************************************
ok: [localhost] => (item=123456)
ok: [localhost] => (item=654321)

TASK [Debug] *****************************************************************************************************************************************************************************
ok: [localhost] => {
   "all_users": [
       {
           "UserID": "654321",
           "UserType": "TBD",
           "UserName": "TBD"
       },
       {
           "UserID": "{{ item }}",
           "UserType": "TBD",
           "UserName": "TBD"
       }
   ]
}

我不確定我錯過了什麼。我嘗試在循環任務之前執行“set_fact”以首先執行 split(),但這沒有幫助。我也試過“with_items”而不是循環,沒有運氣

不要使用嵌套的花括號"{{ ... {{ item }} ... }}"。正確的語法是

all_users: "{{ all_users+ [{ 'UserID': item,
                            'UserType': 'TBD',
                            'UserName': 'TBD' }] }}"

接下來,送出一個列表更方便userids。那麼split過濾器就沒有必要了。例如劇本

shell> cat playbook.yml
- hosts: localhost
 tasks:
   - name: Initiate User List
     set_fact:
       all_users: []
   - name: Add new JSON Objects to List
     set_fact:
       all_users: "{{ all_users +
                      [{ 'UserID': item,
                         'UserType': 'TBD',
                         'UserName': 'TBD' }] }}"
     loop: "{{ userids }}"
   - name: Debug
     debug:
       var: all_users

shell> ansible-playbook -e "userids=['123456','654321']" playbook.yml
...
   "all_users": [
       {
           "UserID": "123456",
           "UserName": "TBD",
           "UserType": "TBD"
       },
       {
           "UserID": "654321",
           "UserName": "TBD",
           "UserType": "TBD"
       }
   ]

userids如果屬性UserID應儲存為integertype 而不是. 從列表中的項目中刪除單引號string。例如

shell> ansible-playbook -e "userids=[123456,654321]" playbook.yml

給出(的值周圍沒有引號UserID

   "all_users": [
       {
           "UserID": 123456,
           "UserName": "TBD",
           "UserType": "TBD"
       },
       {
           "UserID": 654321,
           "UserName": "TBD",
           "UserType": "TBD"
       }
   ]

然後使用字典創建使用者列表。例如,

shell> cat playbook.yml
- hosts: localhost
 vars:
   my_users:
     123456:
       name: Alice Admin
       type: admin
     654321:
       name: Bob Business
       type: sales
 tasks:
   - name: Initiate User List
     set_fact:
       all_users: []
   - name: Add new JSON Objects to List
     set_fact:
       all_users: "{{ all_users +
                      [{ 'UserID': item,
                         'UserType': my_users[item].type,
                         'UserName': my_users[item].name }] }}"
     loop: "{{ userids }}"
   - name: Debug
     debug:
       var: all_users

shell> ansible-playbook -e "userids=[123456,654321]" playbook.yml
...
   "all_users": [
       {
           "UserID": 123456,
           "UserName": "Alice Admin",
           "UserType": "admin"
       },
       {
           "UserID": 654321,
           "UserName": "Bob Business",
           "UserType": "sales"
       }
   ]

引用自:https://serverfault.com/questions/1016863