Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible 1.9.1 'become' and sudo issue

I am trying to run an extremely simple playbook to test a new Ansible setup.

When using the 'new' Ansible Privilege Escalation config options in my ansible.cfg file:

[defaults]

host_key_checking=false

log_path=./logs/ansible.log
executable=/bin/bash

#callback_plugins=./lib/callback_plugins

######

[privilege_escalation]
become=True
become_method='sudo'
become_user='tstuser01'
become_ask_pass=False

[ssh_connection]
scp_if_ssh=True

I get the following error:

fatal: [webserver1.local] => Internal Error: this module does not support running commands via 'sudo'

FATAL: all hosts have already failed -- aborting

The playbook is also very simple:

# Checks the hosts provisioned by midrange
---
- name: Test su connecting as current user
  hosts: all
  gather_facts: no
  tasks:
  - name: "sudo to configued user -- tstuser01"
    #action: ping
    command: /usr/bin/whoami

I am not sure if there is something broken in Ansible 1.9.1 or if I am doing something wrong. Surely the 'command' module in Ansible allows running commands as sudo.

like image 435
ilium007 Avatar asked Apr 30 '15 11:04

ilium007


People also ask

How do you mention sudo privilege in Ansible?

To specify a password for sudo, run ansible-playbook with --ask-become-pass ( -K for short). If you run a playbook utilizing become and the playbook seems to hang, most likely it is stuck at the privilege escalation prompt. Stop it with CTRL-c , then execute the playbook with -K and the appropriate password.

Does Ansible run as sudo?

What is Ansible Sudo? In Ansible, we can use become to make use to Linux system's sudo feature. This makes one user to execute commands on system as another user for the moment of command execution.

How do I add sudo to Ansible?

To create a user with sudo privileges is to put the user into /etc/sudoers , or make the user a member of a group specified in /etc/sudoers . And to make it password-less is to additionally specify NOPASSWD in /etc/sudoers . And instead of fiddling with /etc/sudoers file, we can create a new file in /etc/sudoers.

Does Ansible need sudo?

If you expect ansible to perform tasks that require root access, then ansible needs root privileges, either via sudo or via appropriate ssh credentials to the root account. You can't restrict Ansible to particular commands because Ansible isn't running specific commands; it's running (typically) python .


1 Answers

The issue is with configuration; I also took it as an example and got the same problem. After playing awhile I noticed that the following works:

1) deprecated sudo:

---
- hosts: all
  sudo: yes
  gather_facts: no
  tasks:
  - name: "sudo to root"
    command: /usr/bin/whoami

2) new become

---
- hosts: all
  become: yes
  become_method: sudo
  gather_facts: no
  tasks:
  - name: "sudo to root"
    command: /usr/bin/whoami

3) using ansible.cfg:

[privilege_escalation]
become = yes
become_method = sudo

and then in a playbook:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: "sudo to root"
    command: /usr/bin/whoami

since you "becoming" tstuser01 (not a root like me), please play a bit, probably user name should not be quoted too:

  become_user = tstuser01

at least this is the way I define remote_user in ansible.cfg and it works... My issue resolved, hope yours too

like image 92
Maxym Avatar answered Sep 22 '22 06:09

Maxym