Create users and groups using with_files

33 views
Skip to first unread message

Anfield

unread,
Jul 6, 2017, 8:34:09 PM7/6/17
to Ansible Project
Trying to create groups and then users using with files and its not working..How should the lists be represented in the groups vars file and the users vars file?

It doesnt like it just as text below..how am I supposed to list the values out? How do people normally create a few users and groups like this?

groups.yml
devops
dbadmin
business

users.yml
tom
dave
joe


Playbook
--- # Users and Groups
- hosts: localhost
  vars_files:
    - groups.yml
    - users.yml
  tasks:
    - name: Create groups
      group:
        name: "{{ item }}"
        state: present
      with_file:
        - groups.yml
    - name: Create users
      user:
        name: "{{ item }}"
        state: present
      with_file:
        - users.yml

Daniel JD

unread,
Jul 7, 2017, 3:25:29 PM7/7/17
to Ansible Project
Your files are not in YAML- Syntax. Ansible should give you an error if you start the playbook. You dont event need it at all.

Use with_lines! Like this:


  tasks:
    - name: Create groups
      group:
        name: "{{ item }}"
        state: present
      with_lines: cat groups.yml


    - name: Create users
      user:
        name: "{{ item }}"
        state: present
      with_lines: cat users.yml

Brian Coca

unread,
Jul 7, 2017, 6:18:15 PM7/7/17
to Ansible Project
The 'file' lookup does not deal with YAML so with_files will just
return an entry for the content of the file, 'lines' or split() should
make it work.

Since you don't specify what "doesn't work" means ... I really cannot
even guess at what the error is, always include the error you get when
you are trying to have others help debugging.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages