【问题标题】:Ansible multiple variable contains multiple valuesAnsible 多变量包含多个值
【发布时间】:2019-12-18 09:34:49
【问题描述】:

我在下面有一些可靠的任务:

    - name: counting object from json
     shell: >
      jq '.results[].stdout_lines | length' backup/{{ inventory_hostname }}_rsl.json | wc -l
     register: jsondata
   - name: counting object converting
     shell: >
      seq 0 {{ jsondata.stdout|int - 1 }} | tr '\n' ' ' | xargs | sed 's/[[:space:]]/,/g'
     register: seq
   - name: get interface
     shell: >
      cat backup/{{ inventory_hostname }}_{{ item }}_rsl_result_nows.json | cut -d, -f1
     register: interface
     with_items:
      - "{{ seq.stdout.split(',') | list }}"
   - name: get rsl value
     shell: >
      cat backup/{{ inventory_hostname }}_{{ item }}_rsl_result_nows.json | cut -d, -f2-
     register: rslvalue
     with_items:
      - "{{ seq.stdout.split(',') | list }}"
   - name: post to DB via curl
     shell: >
      curl -d "ip_address={{ inventory_hostname }}&hostname={{ varhostname.stdout }}&interface={{ interface }}&rslvalue={{ rslvalue }}" -X POST http://dev.trm.net:8088/ip_planning/rsl/postrsl -v

我想发布具有相同主机名但不同接口和其他一些属性的数据。

这是我想要的 curl 命令:

curl -d "ip_address=IP_A&hostname=HOST_A&interface=IFACE_1&rslvalue=1,2,3,4,5" -X POST http://dev.trm.net:8088/ip_planning/rsl/postrsl -v

curl -d "ip_address=IP_A&hostname=HOST_A&interface=IFACE_2&rslvalue=5,4,3,2,1" -X POST http://dev.trm.net:8088/ip_planning/rsl/postrsl -v

curl -d "ip_address=IP_B&hostname=HOST_B&interface=IFACE_1&rslvalue=11,21,31,41,51" -X POST http://dev.trm.net:8088/ip_planning/rsl/postrsl -v

我累了一整天,请帮帮我。我已经从上面的 ansible 脚本中尝试过,但是错误

"msg": "任务包含一个带有未定义变量的选项。错误是:'item' is undefined

【问题讨论】:

  • 对于哪个任务你会得到这个错误
  • 最后一个任务“通过 curl 发布到数据库”
  • 任务中有 {{ item.interface.stdout }},但 with_items 不存在
  • 如何使用多个 with_items ?我想在“interface={{ item }}”和“rslvalue={{ item }}” curl 命令中使用具有不同值的“item”变量

标签: bash variables ansible


【解决方案1】:

您在最后一个任务中有{{ item.interface.stdout }},但 with_items 不存在。 此外,当您在 shell 命令中使用 item 时,请使用 "{{item}}"

【讨论】:

【解决方案2】:

一点点改进让我变得有趣......

   - name: post to DB via curl
     shell: >
      curl -d "ip_address={{ inventory_hostname }}&hostname={{ varhostname.stdout }}&interface={{ item }}&rslvalue={{ item }}" -X POST http://dev.tr$
     with_items:
      - "{{ rslvalue.results | map(attribute='stdout') | list }}"
      - "{{ interface.results | map(attribute='stdout') | list }}"

但“item”变量总是打印“rslvalue”结果。如何用多个 with_items 声明“item”??

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2014-06-08
    相关资源
    最近更新 更多