如何在没有双引号的情况下获得ansible输出

2024-09-28 01:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

Var文件/iterate_hosts.yml

---
hostlist:
  - host: '2019'
    inventory_name: 'Level 1'
    ip: '2019'
    mac: 'All industries'
  - host: '2020'
    inventory_name: 'Level 2'
    ip: '2020'
    mac: 'All industries'
  - host: '2021'
    inventory_name: 'Level 3'
    ip: '2021'
    mac: 'All industries'

main.yml

---
- name: csv
  hosts: localhost
  gather_facts: no
  tasks:
   - name: var
     include_vars: ./iterate_hosts.yml
   - name: includeing role
     with_sequence: 0-10
     include_role:
       name: csv_test

Ansible角色(csv_测试主文件)csv_测试/task/main.yml

---
- set_fact:
    count123: '{{item}}'
- name: test
  debug:
    msg: "{{ hostlist[item]}}"

错误:

"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute '0'\n\nThe error appears to be in '/csv/csv_test/tasks/main.yml

意见:

  • 对于第一次迭代,{{ hostlist[count123] }}中的值应该是{{ hostlist[0] }}",依此类推
  • 但是Ansible将0作为字符串“0”
ok: [localhost] => {
    "ansible_facts": {
        "count123": "0"
    },
    "changed": false
}

如果值类似于"count123": 0

如何转义引号并获得整数而不是字符串

期望值:我必须遍历/iterate_hosts.yml,为此,我正在使用main.yml文件中的with_sequence: 0-10

因此:

  • 对于hostlist[count123](第一次迭代应该是count123=0),它应该打印内容表单/iterate\u hosts.yml

    host: '2019'
    inventory_name: 'Level 1'
    ip: '2019'
    mac: 'All industries'
    
  • 对于hostlist[count123](第二次迭代应该是count123=1),它应该打印

    host: '2020'
    inventory_name: 'Level 2'
    ip: '2020'
    mac: 'All industries'
    
  • 等等


Tags: csvnameiphostmainmacymlall
1条回答
网友
1楼 · 发布于 2024-09-28 01:31:18

您可以使用^{}Jinja过滤器进行此操作

因此,您的文件csv_test/task/main.yml最终是:

- set_fact:
    count123: "{{ item | int }}"
- debug:
    msg: "{{ hostlist[item | int] }}" 

注意,with_sequence正在生成字符串这一事实在文档

  • Generated items are strings. Use Jinja2 filters to convert items to preferred type, e.g. {{ 1 + item|int }}

来源:https://docs.ansible.com/ansible/devel/collections/ansible/builtin/sequence_lookup.html#synopsis


这是一本完整的工作手册:

- hosts: localhost
  gather_facts: no
      
  tasks:
    - debug:
        msg: "{{ hostlist[item | int] }}" 
      with_sequence: 0-2
      vars:
        hostlist:
          - host: '2019'
            inventory_name: 'Level 1'
            ip: '2019'
            mac: 'All industries'
          - host: '2020'
            inventory_name: 'Level 2'
            ip: '2020'
            mac: 'All industries'
          - host: '2021'
            inventory_name: 'Level 3'
            ip: '2021'
            mac: 'All industries'

这让我们概括一下:

PLAY [localhost] **************************************************************************************************

TASK [debug] ******************************************************************************************************
ok: [localhost] => (item=0) => {
    "msg": {
        "host": "2019",
        "inventory_name": "Level 1",
        "ip": "2019",
        "mac": "All industries"
    }
}
ok: [localhost] => (item=1) => {
    "msg": {
        "host": "2020",
        "inventory_name": "Level 2",
        "ip": "2020",
        "mac": "All industries"
    }
}
ok: [localhost] => (item=2) => {
    "msg": {
        "host": "2021",
        "inventory_name": "Level 3",
        "ip": "2021",
        "mac": "All industries"
    }
}

PLAY RECAP ********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

相关问题 更多 >

    热门问题