将调试信息保存到变量列表中

时间:2019-02-12 11:18:58

标签: amazon-web-services ansible

我想一次创建多个AWS帐户,并将不同的输出列表保存到变量中。

我尝试使用调试消息进行调试,但可能不是正确的选择。 这个想法如下:

-
  name: Create accounts.
  hosts: localhost

  vars_prompt:
    - name: "tag_start"
      prompt: "Please set the starting number for the account"
      private: no

    - name: "tag_end"
      prompt: "Please set the ending number for the account"
      private: no

  tasks:
    - name: "Emails"
      debug: 
        msg: "test+{{item}}@gmail.com"
      with_sequence: start={{ tag_start }} end={{ tag_end }}
      register: email

    - name: "Account name"
      debug: 
        msg: "account{{item}}"
      with_sequence: start={{ tag_start }} end={{ tag_end }}
      register: account_name

    - name: "Emails list"
      debug: 
        msg: "{{email}}"

    - name: "Account names"
      debug: 
        msg: "{{account_name}}"

    - name: Create AWS account
      shell: >
        aws organizations create-account --email "{{ item[0] }}" \
        --account-name "{{ item[1] }}" \
        --role-name admin \
        --iam-user-access-to-billing ALLOW \
        --profile default
      with_together: 
        - "{{ email }}"
        - "{{ account_name }}"

关键是第一和第二任务似乎按预期工作,仅显示我需要的内容:

TASK [Emails] ********************************************************************************************************************************************************************************************************************************************************************************************************
task path: /Users/me/repos/create_aws_account.yaml:15
ok: [localhost] => (item=46) => {
    "msg": "test+46@gmail.com"
}
ok: [localhost] => (item=47) => {
    "msg": "test+47@gmail.com"
}

TASK [Account name] **************************************************************************************************************************************************************************************************************************************************************************************************
task path: /Users/me/create_aws_account.yaml:21
ok: [localhost] => (item=46) => {
    "msg": "account46"
}
ok: [localhost] => (item=47) => {
    "msg": "account47"
}

但是当我用保存的变量的另一个调试消息检查输出时,得到以下信息:

TASK [Emails] ********************************************************************************************************************************************************************************************************************************************************************************************************
task path: /Users/me/create_aws_account.yaml:27
ok: [localhost] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "46",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "46",
                "msg": "test+46@gmail.com"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "47",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "47",
                "msg": "test+47@gmail.com"
            }
        ]
    }
}

TASK [Account names] *************************************************************************************************************************************************************************************************************************************************************************************************
task path: /Users/me/create_aws_account.yaml:31
ok: [localhost] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "46",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "46",
                "msg": "account46"
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "47",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_verbose_always": true,
                "changed": false,
                "failed": false,
                "item": "47",
                "msg": "account47"
            }
        ]
    }
}

那我该怎么做,只保存msg或stdout? 我也尝试过使用set_fact,但它似乎也不起作用。

2 个答案:

答案 0 :(得分:0)

一种选择是使用 set_fact

创建列表
def dynamic_method(self):
    return {
        <value to run foo1>: foo1,
        <value to run foo2>: foo2,
        <value to run foo3>: foo3
    }[self.method_variation](self)

def foo1(self):
    pass

def foo2(self):
    pass

def foo3(self):
    pass

答案 1 :(得分:0)

最后找到了我想要的东西,并在这里找到了,这是用户KaffeeKiffer的答复。 (第二个答复)。

Link

最后是以下内容:

<img :src="require(`@/assets/${topnews.img}`)" alt="Top News" />

出于完整性考虑: list是必需的,因为map将始终返回迭代器。