【问题标题】:What is the best way to do case statement in Ansible?在 Ansible 中做案例陈述的最佳方法是什么?
【发布时间】:2019-10-13 03:20:19
【问题描述】:

我正在尝试将我的脚本转换为 Ansible 以实现自动化。我一直在理解循环或“with_items”用例。

原始 bash 脚本:

for i in apple banana orange; do
    case $i in
      apple) export var="var.1:apple1,var.2:apple2" ;;
      banana) export var="var.1:banana1,var.2:banana2,var.3:banana3" ;;
      orange) export var="var.1:orange1" ;;
    esac

    echo "$i"

到目前为止我所尝试的:

VARS file:
fruits:
  - name: apple
    var: "{{ item }}"
    with_items:
      - apple1
      - apple2
  - name: banana
    var: "{{ item }}"
    with_items:
      - banana1
      - banana2
      - banana3
  - name: orange
    var: "{{ item }}"
    with_items:
      - orange1
TASKS file:
- include_vars: vars.yml

- debug:
    msg: "{{ fruits }}"

- name: output in shell using echo 
  shell: |
    echo "{{ fruits.name }}" ;
    echo "{{ fruits.var }}"
  loop: "{{ fruits }}"

输出:

The output from include_vars task:
{
    "ansible_included_var_files": [
        "/etc/ansible/roles/openssl/tasks/vars.yml"
    ],
    "ansible_facts": {
        "fruits": [
            {
                "var": "{{ item }}",
                "name": "apple",
                "with_items": [
                    "apple1",
                    "apple2"
                ]
            },
            {
                "var": "{{ item }}",
                "name": "banana",
                "with_items": [
                    "banana1",
                    "banana2",
                    "banana3"
                ]
            },
            {
                "var": "{{ item }}",
                "name": "orange",
                "with_items": [
                    "orange1"
                ]
            }
        ]
    },
    "_ansible_no_log": false,
    "changed": false
}

调试

debug task failed
{
    "msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/etc/ansible/roles/openssl/tasks/main.yml': line 262, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n  ^ here\n",
    "_ansible_no_log": false
}

我知道 Yaml 对空间敏感,格式很重要。我是编写剧本的新手,任何建议都会有所帮助。

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    dictionary

    fruits:
      apple:
        - apple1
        - apple2
      banana:
        - banana1
        - banana2
        - banana3
      orange:
        - orange1
    

    loop

    - hosts: localhost
      tasks:
        - include_vars:
            vars.yml
        - debug:
            msg: "{{ item.key }} {{ item.value }}"
          loop: "{{ fruits|dict2items }}"
    

    给予(删节)

    "msg": "orange [u'orange1']"
    "msg": "apple [u'apple1', u'apple2']"
    "msg": "banana [u'banana1', u'banana2', u'banana3']"
    

    可以引用字典中的项目。例如

    - debug:
        var: fruits.banana
    - debug:
        var: fruits.apple.1
    

    给予

    "fruits.banana": [
        "banana1", 
        "banana2", 
        "banana3"
    ]
    
    "fruits.apple.1": "apple2"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-11
      • 2017-07-03
      • 1970-01-01
      • 2023-04-10
      • 2010-10-30
      • 2016-05-13
      • 2016-02-21
      相关资源
      最近更新 更多