简体   繁体   English

在ansible剧本中声明变量的正确方法

[英]proper way to declare variable in ansible playbook

I have the following playbook: 我有以下剧本:

  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}}"

This returns the following results: 这将返回以下结果:

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#

I've been playing around between set_fact and "vars" but I can't seem to get this going. 我一直在set_fact和“ vars”之间玩耍,但似乎无法解决这个问题。 As you can see, the debug statement on line 11 results in the list you see below... which seems to be work... and which seems to prove that I set the variable correctly. 如您所见,第11行上的debug语句导致您在下面看到的列表中……这似乎是可行的……并且似乎证明了我正确设置了变量。 I'm not sure what else to try. 我不确定还有什么尝试。

Thanks. 谢谢。

EDIT 1 编辑1

This is what my code looks like for that specific section: 这是该特定部分的代码:

 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}}"

When I run it, i get the following error message: 运行它时,出现以下错误消息:

ERROR! 错误! Syntax Error while loading YAML. 加载YAML时的语法错误。

The error appears to have been in '/etc/ansible/playbooks/lxc_container_test.yml': line 20, column 7, but may be elsewhere in the file depending on the exact syntax problem. 错误似乎出在'/etc/ansible/playbooks/lxc_container_test.yml'中:第20行,第7列,但根据确切的语法问题,可能在文件的其他位置。

The offending line appears to be: 令人反感的行似乎是:

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

Line 20 is indented 2 spaces compared to the "with_items" line on line 17 与第17行的“ with_items”行相比,第20行缩进了2个空格

Your playbook is incorrect in line 15-22 (specifically the indentation and the quotes), the correct form is: 您的剧本在15-22行中有误(特别是缩进和引号),正确的格式为:

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

The Run memory command within "running" container task has incorrect indentation. Run memory command within "running" container任务中的Run memory command within "running" container缩进不正确。

with_items and register are both properties of the Ansible task, not properties of the lxc_container module, so they should be indented inline with the Ansible task properties. with_itemsregister都是Ansible任务的属性,而不是lxc_container模块的属性,因此应与Ansible任务属性内联缩进。

Original: 原版的:

 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

Corrected: 已更正:

        # 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

Here is the corrected code. 这是更正的代码。 You have define the variable correctly. 您已经正确定义了变量。 set-fact is used to define a global variable within the play. set-fact用于在剧本中定义全局变量。 Only the indentation of with_items was not correct. with_items的缩进不正确。 Now it should work 现在应该可以了

 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}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM