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

How to get the ansible output without Double Quotes

可变文件./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_test主文件)csv_test/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

观察:

ok: [localhost] => {
    "ansible_facts": {
        "count123": "0"
    },
    "changed": false
}

如果值类似于 "count123": 0

,则此解决方案有效

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

期望:我必须遍历 ./iterate_hosts.yml 并且为此我在我的 [ 中使用 with_sequence: 0-10 =79=] 文件.

所以:

您可以为此使用 int 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