proper way to declare variable in ansible playbook

ansible variables example
ansible vars file example
ansible set variable in task
ansible variables list
ansible global variables
ansible inventory variables
ansible assign variable to another variable
ansible include_vars

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. 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. I'm not sure what else to try.

Thanks.

EDIT 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.

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.

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

Your playbook is incorrect in line 15-22 (specifically the indentation and the quotes), the correct form is:

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

Using Variables — Ansible Documentation, See the Inventory document for multiple ways on how to define variables in inventory. This can be nice as it's right there when you are reading the playbook. As discussed in the playbooks chapter, Ansible facts are a way of getting data about remote systems for use in playbook variables. Usually these are discovered automatically by the setup module in Ansible. Users can also write custom facts modules, as described in the API guide.

The Run memory command within "running" container task has incorrect indentation.

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.

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

Variables — Ansible Documentation, Your playbook is incorrect in line 15-22 (specifically the indentation and the quotes), the correct form is: lxc_container: name: {{ item }}� Variables set by passing -e var=value to ansible-playbook have the highest precedence, which means you can use this to override variables that are already defined. Example 4-11 shows how to set the variable named token to the value 12345 .

Here is the corrected code. You have define the variable correctly.set-fact is used to define a global variable within the play. Only the indentation of with_items was not correct. 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}}"

proper way to declare variable in ansible playbook, Variables in Inventory files. If you have different host systems that share similar attributes or values, you can define what we call group variables. In this example we declare 3 variable, 1th is “base_path” and set value /var/www/html/, this is our apache directory path where we create a directory using variable “ansible_hostname” , 2nd variable is index_file, we create a index.html and 3rd index_msg, we echo a message in index file.

How to Use Variables in Ansible Playbook, Defining Variables in Playbooks Let's say we wanted to take the preceding example and put the variables in a file named nginx.yml instead of putting them right in the As we discussed in Chapter 3, Ansible also let you define variables We saw in Chapter 2 how we could use the debug module to print� Ansible Tower’s Survey one of the cool feature which can be used to populate the variables every time the Template is executed. Ansible playbook might contain one more user variable depends on the use cases. If you want to attach such playbook in AWX / Ansible Tower template, you need to pass the variables in the “ EXTRA VARIABLES” box

4. Variables and Facts - Ansible: Up and Running [Book], How to Define and Use Variables in Ansible Playbook, in this video we use Variable in Duration: 13:23 Posted: 1 May 2019 Ansible Galaxy is a free site for finding, downloading, rating, and reviewing all kinds of community developed Ansible roles and can be a great way to get a jumpstart on your automation projects. The client ansible-galaxy is included in Ansible.

How to Define and Use Variables in Ansible Playbook, You can define variables directly in a playbook: - hosts: webservers vars: ansible playbook: using multiple variables in loops How do I access environment variables in Ansible? Yes @Nagray, you're right! You can access� About Playbooks ¶. Playbooks are a completely different way to use ansible than in ad-hoc task execution mode, and are particularly powerful. Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.

Comments
  • register and with_items need to be un-indented by two spaces to put them in line with lxc_container. I tried editing the post, but stackoverflow doesn't allow 4-char whitespace edits...
  • And {{ item }} needs quotes to keep it from being a YAML dictionary, e.g: name: "{{ item }}"
  • hi. i tried your suggestion but I'm getting a syntax error. Please see EDIT 1
  • For corrected syntax, see the answer here: stackoverflow.com/a/39685471/6841451