how to get value from yaml file ?

39 views
Skip to first unread message

Eric Chang

unread,
Nov 22, 2018, 8:08:01 AM11/22/18
to Ansible Project
I want to run a playbook to create an individual folder for users in different groups

and I already create a teams.yml file like below
        {
            "chinese_name": "\u8ca1\u52d9\u6703\u8a08\u8655",
            "description": "\u8ca1\u52d9\u6703\u8a08\u8655",
            "gid": 10126,
            "location": [
                "hq"
            ],
            "name": "ac",
            "users": [
                "tsengyi",
                "chenp",
                "lich",
                "hsiaoch",
                "wangr",
                "dengm",
                ""guot". 
            ]


then I wrote a task
 ### load yaml file first
- name: load teams.yml                                                                                                                                          
  tags:
    - env
  include_vars:
    file: files/konwen/teams.yml

- name: create folders
  file:
    path: "/home/{{item.user}}/D"
    state: present
  when:  item.name ==  'ac'
  with_items: "{{ teams}}"
 
 but the result 
 tsengyi
'[u'\''tsengyi'\'', u'\''chenp'\'', u'\''lich'\'', u'\''hsiaoch'\'', u'\''wangr'\'', u'\''dengm'\'', u'\''guot'\'', u'\''hsul'\'', u'\''linm'\'']'
 wangc

looks like ansible take whole list  into a single value

I expect to create /home/tsengyi/D , /home/chenp/D .....and so on

How can I separate the list into piece to do what I need ??


Uwe Sauter

unread,
Nov 22, 2018, 2:28:18 PM11/22/18
to ansible...@googlegroups.com
Hi,

your file looks like JSON to me, not like YAML (though one is a subset of the other, if I remember correctly…).

My test:

### teams.yaml ###
---
chinese_name: "\u8ca1\u52d9\u6703\u8a08\u8655"
description: "\u8ca1\u52d9\u6703\u8a08\u8655"
gid": 10126
location":
- "hq"
name: "ac"
users:
- tsengyi
- chenp
- lich
- hsiaoch
- wangr
- dengm
- guot
##################

### test.yaml ###
---
- hosts: localhost
gather_facts: false
tasks:
- include_vars: teams.yaml
- debug:
msg: '{{ item }}'
loop: '{{ users }}'
#################

results in:

$ ansible-playbook test.yaml

PLAY [localhost] ***************************************************************

TASK [include_vars] ************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => (item=tsengyi) =>
msg: tsengyi
ok: [localhost] => (item=chenp) =>
msg: chenp
ok: [localhost] => (item=lich) =>
msg: lich
ok: [localhost] => (item=hsiaoch) =>
msg: hsiaoch
ok: [localhost] => (item=wangr) =>
msg: wangr
ok: [localhost] => (item=dengm) =>
msg: dengm
ok: [localhost] => (item=guot) =>
msg: guot

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



I you want the content of teams.yaml to be inside variable "teams" you need to tell that Ansible:

tasks:
- include_vars:
file: teams.yaml
name: teams

That way, you can refer to teams["users"] and not just to users as in my test.yaml.




Am 22.11.18 um 14:08 schrieb Eric Chang:
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To post to this group, send email to ansible...@googlegroups.com <mailto:ansible...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4e7e34a9-48b8-4d6a-bd82-d04361dfeaca%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/4e7e34a9-48b8-4d6a-bd82-d04361dfeaca%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Eric Chang

unread,
Nov 23, 2018, 12:37:34 AM11/23/18
to Ansible Project
thanks for reply , I think my source file is yaml file
I can pass the yaml validator online

here are 2 items in teams.yml

"teams": [
    {"chinese_name": "\u79d8\u66f8\u90e8","description": "\u79d8\u66f8\u90e8","gid": 10125,"location": ["hq"],"name": "aa","users": ["chenhu","chouf"]},
    {"chinese_name": "\u8ca1\u52d9\u6703\u8a08\u8655","description": "\u8ca1\u52d9\u6703\u8a08\u8655","gid": 10126,"location": ["hq"],"name": "ac","users": ["chenchiao","chengc"]}
    ]

even I assign a name in task , I still can not use users with tasks below
- name: load teams.yml                                                                                                                
  tags:
    - env
  include_vars:
    file: files/teams.yml
    name: teams

- name: debug
  tags:
    - env

  debug:
    msg: "{{ item }}"
  loop: " {{ users }}"


I can print the {{ teams }} , but can not get valule from {{ team.users }} or {{ team.name }}
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'name' is undefined / The error was: 'chinese_name' is undefined


Eric Chang於 2018年11月22日星期四 UTC+8下午9時08分01秒寫道:

Uwe Sauter

unread,
Nov 23, 2018, 1:20:48 AM11/23/18
to Eric Chang, Ansible Project
What version of Ansible?

Are you sure the files are encoded in UTF-8?
--
Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.
Reply all
Reply to author
Forward
0 new messages