Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Ansible didn't see attribute in variable?

I have Ansible role "db" with simple task:

- name: Check repos
  apt_repository: repo="{{ item.repo }}" state={{ item.state }}
  with_items:
  - "{{ apt_repos }}"

In /defaults/mail.yml:

apt_repos:

   # Percona
 - { state: present, repo: 'deb http://repo.percona.com/apt wheezy main', keyserver: 'keyserver.ubuntu.com', key: '1C4CBDCDCD2EFD2A', needkey: True }
 - { state: present, repo: 'deb-src http://repo.percona.com/apt wheezy main', needkey: False }

When i try to run this ansible-playbook:

---
- hosts: test
  roles:
  - db

i see error:

fatal: [10.10.10.10] => One or more undefined variables: 'unicode object' has no attribute 'repo'

FATAL: all hosts have already failed -- aborting

But i have another role with same task and variable and it work perfectly. What's wrong?

like image 477
UsCr Avatar asked Oct 23 '15 13:10

UsCr


People also ask

How do you call a variable in ansible?

Begin by calling the vars keyword then call the variable name followed by the value as shown. In the playbook above, the variable name is salutations and the value is Hello world! When the playbook is run, the value of the playbook is accessed by placing the variable between curly braces as shown above.

How do I add VARS to ansible playbook?

The include_vars module can be used in a playbook or role to load variables from a file. Simply set the value of include_vars to a local file to load the variables it contains: --- # ./hello_world. yml - name: print greeting hosts: "*" tasks: - include_vars: name_vars.


1 Answers

You want to be doing this:

with_items: apt_repos

apt_repos is a list. By referencing it as - "{{ apt_repos }}" the extra - is turning it into a list of lists. You also don't need the quotes or braces in this case - those are pretty much just redundant in this type of situation.

like image 70
Bruce P Avatar answered Oct 11 '22 12:10

Bruce P