【问题标题】:How to use ansible loop如何使用 ansible 循环
【发布时间】:2020-06-23 06:41:58
【问题描述】:

变量文件

我有一个变量文件

fruits:
  - apple
  - banana
  - orange
  - strawberry
  - grapes
  - papaya

users:
  - user_name: 'john'
    user_age: 45
  - user_name: 'yash'
    user_age: 95
  - user_name: 'srk'
    user_age: 52
  - user_name: 'alia'
    user_age: 26

剧本任务

还有我的 ansible 任务,只是尝试创建一个文本文件并在文件中以垂直顺序添加变量。

- hosts: localhost
  gather_facts: true
  vars_files:
    - variables.var # this is my variable file in the same dir that playbook have.
  tasks:
    - name: add fruits to the list
      lineinfile:
        create: yes
        line: "{{ item }}"
        path: /home/ansible/ansible-demo2/fruits.txt
      loop:
        - "{{ fruits|flatten }}"

    - name: add uses to the list
      lineinfile:
        create: yes
        line: "{{ item.user_name }} ==> {{ item.user_age }}"
        path: /home/ansible/ansible-demo2/users.txt
      loop:
        - "{{ users|flatten(levels=1) }}"

错误

但我的行为很奇怪。下面是 fruits 任务的输出和 users 任务的错误。

TASK [add fruits to the list] ***************************************************************************************************************************
ok: [localhost] => (item=[u'apple', u'banana', u'orange', u'strawberry', u'grapes', u'papaya'])
[WARNING]: The value ['apple', 'banana', 'orange', 'strawberry', 'grapes', 'papaya'] (type list) in a string field was converted to u"['apple',
'banana', 'orange', 'strawberry', 'grapes', 'papaya']" (type string). If this does not look like what you expect, quote the entire value to ensure it
does not change.

TASK [add uses to the list] *****************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'user_name'
\n\nThe error appears to be in '/home/ansible/ansible-demo2/myansible.yml': line 18, column 7, but may\nbe elsewhere in the file depending on the exact s
yntax problem.\n\nThe offending line appears to be:\n\n\n    - name: add uses to the list\n      ^ here\n"}

预期输出

在文本文件中按垂直顺序列出。

对问题的评论:

我很惊讶with_itemsloop 的每件事都能正常工作,即使user_name 变量已定义,但它仍然显示未定义。现在我无法找出发生了什么问题。

参考:

这是我引用的文档:Migrating from with_X to loop

编辑:变量调试输出

我调试变量。输出低于

TASK [debug] ********************************************************************************************************************************************
ok: [localhost] => {
    "fruits": [
        "apple",
        "banana",
        "orange",
        "strawberry",        "grapes",        "papaya"
    ]
}

TASK [debug] ********************************************************************************************************************************************
ok: [localhost] => {
    "users": [
        {
            "user_age": 45,
            "user_name": "john"
        },
        {
            "user_age": 95,
            "user_name": "yash"
        },
        {
            "user_age": 52,
            "user_name": "srk"
        },
        {
            "user_age": 26,
            "user_name": "alia"
        }
    ]
}

【问题讨论】:

  • 如何读取带有变量的文件? (group_vars, host_vars, vars_files, include_vars, ...)
  • @VladimirBotka 我正在使用vars_files
  • 变量可能有问题。测试它- debug: var=fruits
  • @VladimirBotka 先生,请检查更新的答案和调试结果
  • 数据的格式好像有问题。 "strawberry", "grapes", "papaya" 不应该在同一行。

标签: loops ansible


【解决方案1】:

Q: *"[WARNING]: 字符串字段中的值 ['apple', ... ](类型列表)被转换为 u"['apple', ... ]"(类型字符串)。

A:从代码来看,并不清楚这种转换的原因是什么。下面的数据和剧本按预期工作。

shell> cat data.yml
fruits:
  - apple
  - banana
  - orange
  - strawberry
  - grapes
  - papaya

users:
  - user_name: 'john'
    user_age: 45
  - user_name: 'yash'
    user_age: 95
  - user_name: 'srk'
    user_age: 52
  - user_name: 'alia'
    user_age: 26
shell> cat playbook.yml
- hosts: localhost
  vars_files:
    - data.yml
  tasks:
    - name: add fruits to the list
      lineinfile:
        create: yes
        line: "{{ item }}"
        path: fruits.txt
      loop: "{{ fruits }}"

    - name: add uses to the list
      lineinfile:
        create: yes
        line: "{{ item.user_name }} ==> {{ item.user_age }}"
        path: users.txt
      loop: "{{ users }}"

给予

shell> cat fruits.txt 
apple
banana
orange
strawberry
grapes
papaya
shell> cat users.txt 
john ==> 45
yash ==> 95
srk ==> 52
alia ==> 26

问:“2 个变量 fruits1 和 fruits2 ... 将它们的数据附加到单个文件中 ...在具有 2 个变量的单个任务中”

A:使用修改后的数据

shell> cat data.yml
fruits1:
  - apple
  - banana
  - orange

fruits2:
  - strawberry
  - grapes
  - papaya

这个任务给出相同的结果

    - name: add fruits to the list
      lineinfile:
        create: yes
        line: "{{ item }}"
        path: fruits.txt
      loop: "{{ fruits1 + fruits2 }}"

Append list variable to another list in Ansible


对问题的评论:“我很惊讶在 with_items 而不是循环的情况下一切正常

答:见Comparing loop and with_*

【讨论】:

  • 我认为一切都一样,但你没有在循环中使用flatten,我仍然遇到同样的错误。
  • 对于您在答案顶部提到的错误,我尝试了this answer,它在文本文件中给了我输出[u'apple', u'banana', u'orange', u'strawberry', u'grapes', u'papaya']
  • 查看变量- debug: msg="{{ fruits|type_debug }}"的类型
  • 我刚刚比较了你的 yml 和我的。我在声明循环变量时发现的差异。导致错误。由于我是 ansible 的新手,你能解释一下最好的方法吗?我有 2 个变量 fruit1 和 fruit2 并想将它们的数据附加到单个文件中,是否可以在单个任务中定义 2 个变量???
猜你喜欢
  • 1970-01-01
  • 2016-03-29
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多