Unable to pass Role/X/Var variables to Task

11 views
Skip to first unread message

Yogi Raghunathan

unread,
Dec 18, 2017, 6:53:38 PM12/18/17
to Ansible Project
Hello Folks,

Trying to do a basic example for roles for generating configs. I cannot get to pass the variable to the tasks. Any help is appreciated. 

$ ansible --version
ansible 2.4.2.0
  config file = /home/cisco/ansible.cfg
  configured module search path = [u'/home/cisco/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]

$ more roles/router/tasks/main.yml
---
  - name: generate configuration files
    template: src=router.j2 dest=/home/cisco/ansible/cfgs/{{item.hostname}}.txt
    with_items: " {{ routers }}"


$ more roles/router/vars/mail.yml
---
 routers:
  - { hostname: router-rtr1 }
  - { hostname: router-rtr2 }
  - { hostname: router-rtr3 }
  - { hostname: router-rtr4 }
  - { hostname: router-rtr5 }
  - { hostname: router-rtr6 }
...

$ ansible-playbook config-gen.yml

PLAY [Generate router configuration files] ****************************************************

TASK [Gathering Facts] ************************************************************************
ok: [localhost]

TASK [router : generate configuration files] **************************************************
fatal: [localhost]: FAILED! => {"msg": "'routers' is undefined"}

PLAY RECAP ************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

$


$ tree
.
├── ansible
│   └── cfgs
│       ├── router-rtr1.txt
│       └── router-rtr2.txt
├── ansible.cfg
├── config-gen.retry
├── config-gen.yml
├── hosts
├── np-perf-playbook-v2.1.yml
├── np-perf-playbook-v2.retry
├── np-perf-playbook-v2.yml
├── playbook-v3.retry
├── playbook-v3.yml
├── roles
│   └── router
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       │   └── router.j2
│       └── vars
│           └── mail.yml
├── site.retry
├── site.yml
├── sr_playbook-v1.yml
├── test.txt
└── test.yml

Thanks,
Yogi

Kai Stian Olstad

unread,
Dec 19, 2017, 12:44:24 AM12/19/17
to ansible...@googlegroups.com
On 19.12.2017 00:53, Yogi Raghunathan wrote:
> $ more roles/router/tasks/main.yml
> ---
> - name: generate configuration files
> template: src=router.j2
> dest=/home/cisco/ansible/cfgs/{{item.hostname}}.txt
> with_items: " {{ routers }}"
>
>
> $ more roles/router/vars/mail.yml
> ---
> routers:
> - { hostname: router-rtr1 }
> - { hostname: router-rtr2 }
> - { hostname: router-rtr3 }
> - { hostname: router-rtr4 }
> - { hostname: router-rtr5 }
> - { hostname: router-rtr6 }

The filename must be main.yml, not mail.yml that you have.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages