'Ansible: Can I execute role from command line?

Suppose I have a role called "apache"

Now I want to execute that role on host 192.168.0.10 from the command line from Ansible host

ansible-playbook -i  "192.168.0.10" --role  "path to role"

Is there a way to do that?



Solution 1:[1]

With ansible 2.7 you can do this:

$ cd /path/to/ansible/
$ ansible localhost -m include_role -a name=<role_name>
localhost | SUCCESS => {
    "changed": false,
    "include_variables": {
        "name": "<role_name>"
    }
}
localhost | SUCCESS => {
    "msg": "<role_name>"
}

This will run role from /path/to/ansible/roles or configured role path.

Read more here: https://github.com/ansible/ansible/pull/43131

Solution 2:[2]

I am not aware of this feature, but you can use tags to just run one role from your playbook.

roles:
    - {role: 'mysql', tags: 'mysql'}
    - {role: 'apache', tags: 'apache'}

ansible-playbook webserver.yml --tags "apache"

Solution 3:[3]

There is no such thing in Ansible, but if this is an often use case for you, try this script.
Put it somewhere within your searchable PATH under name ansible-role:

#!/bin/bash

if [[ $# < 2 ]]; then
  cat <<HELP
Wrapper script for ansible-playbook to apply single role.

Usage: $0 <host-pattern> <role-name> [ansible-playbook options]

Examples:
  $0 dest_host my_role
  $0 custom_host my_role -i 'custom_host,' -vv --check
HELP
  exit
fi

HOST_PATTERN=$1
shift
ROLE=$1
shift

echo "Trying to apply role \"$ROLE\" to host/group \"$HOST_PATTERN\"..."

export ANSIBLE_ROLES_PATH="$(pwd)/roles"
export ANSIBLE_RETRY_FILES_ENABLED="False"
ansible-playbook "$@" /dev/stdin <<END
---
- hosts: $HOST_PATTERN
  roles:
    - $ROLE
END

Solution 4:[4]

You could also check ansible-toolbox repository. It will allow you to use something like

ansible-role --host 192.168.0.10 --gather --user centos --become my-role

Solution 5:[5]

I have written a small Ansible plugin, called auto_tags, that dynamically generates for each role in your playbook a tag of the same name. You can find it here.

After installing it (instructions are in the gist above) you could then execute a specific role with:

ansible-playbook -i "192.168.0.10" --tags "name_of_role"

Solution 6:[6]

Have you tried that? it's super cool. I'm using 'update-os' instead of 'apache' role to give a more meaningful example. I have a role called let's say ./roles/update-os/ in my ./ I add a file called ./role-update-os.yml which looks like:

#!/usr/bin/ansible-playbook
---
- hosts: all
  gather_facts: yes
  become: yes
  roles:
  - update-os

Make this file executable (chmod +x role-update-os.yml). Now you can run and limit to whatever you have in your inventory ./update-os.yml -i inventory-dev --limit 192.168.0.10 the limit you can pass the group names as well.

  • --limit web,db > web and db is the group defined in your inventory
  • --limit 192.168.0.10,192.168.0.201
$ cat inventory-dev
[web]
192.168.0.10

[db]
192.168.0.201

Note that you can configure ssh-keys and sudoers policy to be able to execute without having to type password - ideal for automation, there are security implications with this. therefore you have to analyze your environment to see whether it's suitable.

Solution 7:[7]

Since in ansible 2.4 two options are available: import_role and include_role.

wohlgemuth@leela:~/workspace/rtmtb-ansible/kvm-cluster$ ansible localhost -m import_role -a name=rtmtb
 [WARNING]: No inventory was parsed, only implicit localhost is available

localhost | CHANGED => {
    "changed": true, 
    "checksum": "d31b41e68997e1c7f182bb56286edf993146dba1", 
    "dest": "/root/.ssh/id_rsa.github", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "b7831c4c72f3f62207b2b96d3d7ed9b3", 
    "mode": "0600", 
    "owner": "root", 
    "size": 3389, 
    "src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.46-139127672211209/source", 
    "state": "file", 
    "uid": 0
}
localhost | CHANGED => {
    "changed": true, 
    "checksum": "1972ebcd25363f8e45adc91d38405dfc0386b5f0", 
    "dest": "/root/.ssh/config", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "f82552a9494e40403da4a80e4c528781", 
    "mode": "0644", 
    "owner": "root", 
    "size": 147, 
    "src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.99-214274671218454/source", 
    "state": "file", 
    "uid": 0
}

ansible.builtin.import_role – Import a role into a play

ansible.builtin.include_role – Load and execute a role

Solution 8:[8]

Yes, import_role is an ansible module and as such it may be invoked through ansible command. The following executes role pki on my_server

ansible my_server -m import_role \
                  -a "name=pki tasks_from=gencert" \
                  -e cn=etcdctl \
                  -e extended_key_usage=clientAuth

Solution 9:[9]

You can create the playbook files from the command line:

  1. Install the role (if not already installed)
    ansible-galaxy install git+https://github.com/user/apache-role.git
    
  2. Create playbook and hosts files
    cat >> playbook.yml <<EOL
    ---
    - name: Run apache
      hosts: all
      roles:
        - apache-role
    EOL
    
    cat >> hosts <<EOL
    192.168.0.10
    EOL
    
  3. Run ansible
    ansible-playbook playbook.yml -i hosts
    
  4. Delete the files
    rm playbook.yml hosts
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 abuzze
Solution 3 Konstantin Suvorov
Solution 4 Sasha Miroshnychenko
Solution 5 rkrzr
Solution 6 slm
Solution 7 Bram
Solution 8 olopopo
Solution 9 Noam Nol