【问题标题】:proper way to declare variable in ansible playbook在 ansible playbook 中声明变量的正确方法
【发布时间】:2016-05-20 20:32:03
【问题描述】:

我有以下剧本:

  1 ---
  2 - hosts: lxc_hosts
  3   name:  install software on lxc container
  4   tasks:
  5   - name: get list of containers on lxc host {{inventory_hostname}}
  6     shell: >
  7       lxc-ls | more | tr '\n' ',' | sed 's/,$//'
  8     register: containers
  9   - set_fact:
 10       container_list: "{{ containers.stdout.split(',')|select('match', 'server*')|list }}"
 11   - debug: msg="{{item}}"
 12     with_items:
 13       - "{{container_list}}"
 14   - name: Run memory command within "running" container
 15     lxc_container:
 16       name: "{{item}}"
 17       with_items: 
 18         - "{{container_list}}"
 19       container_command: |
 20         df -h
 21       register: memory_check
 22   - debug: msg="{{memory_check.stdout}}"

这将返回以下结果:

PLAY [install software on lxc container] 
****************************************

TASK [setup]
******************************************************************* 
ok: [10.1.1.1]

TASK [get list of containers on lxc host 10.1.1.1]
************************* 
changed: [10.1.1.1]

TASK [set_fact]
**************************************************************** 
ok: [10.1.1.1]

TASK [debug]
*******************************************************************
ok: [10.1.1.1] => (item=server1-container) => {
    "item": "server1-container", 
    "msg": "server1-container" } 
ok: [10.1.1.1] => (item=server2-container) => {
    "item": "server2-container", 
    "msg": "server2-container" } 
ok: [10.1.1.1] => (item=server3-container) => {
     "item": "server3-container", 
    "msg": "server3-container" }

TASK [Run memory command within "running" container]
*************************** 
fatal: [10.1.1.1]: FAILED! => {"failed": true, "msg": "'item' is undefined"}

NO MORE HOSTS LEFT
*************************************************************   
     to retry, use: --limit @playbooks/inventory_get_containers_on_lxc.retry

PLAY RECAP
*********************************************************************
10.1.1.1               : ok=4    changed=1    unreachable=0    failed=1   

mymachine:/etc/ansible#

我一直在 set_fact 和“vars”之间玩弄,但我似乎无法做到这一点。如您所见,第 11 行的调试语句导致您在下面看到的列表......这似乎是工作......这似乎证明我正确设置了变量。 我不确定还能尝试什么。

谢谢。

编辑 1

这是我的代码在该特定部分的样子:

 14   - name: Run memory command within "running" container
 15     lxc_container:
 16       name: "{{item}}"
 17     with_items:
 18 #         - "{{ containers.stdout.split(',')|select('match', 'server*')|list }}"
 19         - "{{container_list}}"
 20       container_command: |
 21         df -h
 22     register: memory_check
 23   - debug: msg="{{memory_check.stdout}}"

当我运行它时,我收到以下错误消息:

错误!加载 YAML 时出现语法错误。

错误似乎出现在“/etc/ansible/playbooks/lxc_container_test.yml”中:第 20 行第 7 列,但可能 根据确切的语法问题,位于文件的其他位置。

违规行似乎是:

    - "{{container_list}}"
  container_command: |
  ^ here

与第 17 行的“with_items”行相比,第 20 行缩进了 2 个空格

【问题讨论】:

    标签: ansible ansible-playbook ansible-2.x


    【解决方案1】:

    您的剧本在第 15-22 行不正确(特别是缩进和引号),正确的形式是:

    lxc_container:
      name: {{ item }}
      container_command: |
        df -h
      register: memory_check
      with_items: container_list
    

    【讨论】:

    • registerwith_items 需要取消缩进两个空格以使它们与 lxc_container 一致。我尝试编辑帖子,但 stackoverflow 不允许 4 字符空格编辑...
    • 而 {{ item }} 需要引号以防止它成为 YAML 字典,例如:name: "{{ item }}"
    【解决方案2】:

    Run memory command within "running" container 任务的缩进不正确。

    with_itemsregister 都是 Ansible 任务的属性,而不是 lxc_container 模块的属性,因此它们应该与 Ansible 任务属性内联缩进。

    原文:

     14   - name: Run memory command within "running" container
     15     lxc_container:
     16       name: "{{item}}"
              # with_items is a Ansible Task property, so it shouldn't be here
     17       with_items: 
     18         - "{{container_list}}"
     19       container_command: |
     20         df -h
              # register is an Ansible Task property, so it shouldn't be here
     21       register: memory_check
    
    

    更正:

            # Ansible task properties are intented at this level
     14   - name: Run memory command within "running" container
     17     with_items: "{{container_list}}"
     15     lxc_container:
              # lxc_container properties are indented at this level
     16       name: "{{item}}"
     19       container_command: |
     20         df -h
     21     register: memory_check
    

    【讨论】:

      【解决方案3】:

      这是更正后的代码。您已经正确定义了变量。set-fact 用于在剧中定义全局变量。只有with_items 的缩进不正确。现在它应该可以工作了

       15     lxc_container:
       16       name: "{{item}}"
       17     with_items:
       18         - "{{container_list}}"
       19       container_command: |
       20         df -h
       21     register: memory_check
       22   - debug: msg="{{memory_check.stdout}}"
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-17
      • 2016-03-31
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多