user3764430
user3764430

Reputation: 131

Ansible playbook does not run tasks in roles

I have a simple ansible roles with one task, but the problem is when i run it the tasks are not actually started

It worked when I tried my task without roles and not sure why its happening when I try using roles.

Version of ansible: ansible 2.2.3.0

This is my run.yml

 - name: add user to general purpose
   hosts: localhosts
   roles:
    - adduser

cd adduser/tasks/main.yml

- name: Create user
  shell: sudo adduser tom

Running

ansible-playbook run.yml -vvv

This is the output

 Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available


PLAYBOOK: run.yml 
**************************************************************
1 plays in run.yml

PLAY RECAP 
*********************************************************************

Upvotes: 0

Views: 4429

Answers (1)

mdaniel
mdaniel

Reputation: 33158

It is because you have a typo in your hosts: field; the name is localhost not localhosts (as there is no such thing as a plural of the local host)


Also, while this isn't what you asked, it is bad news to (a) manually use sudo in a module (b) call adduser unconditionally, as it will bomb the second time you run that playbook. The thing you want is to tell ansible that task needs elevated privileges and then make use of the user: module to allow ansible to ensure there is such a user by the end of that role:

- name: Create user
  become: yes
  user:
    name: tom

The benefit of being more declarative is (a) that's how ansible works (b) it allows ansible to be idempotent across runs

Upvotes: 1

Related Questions