skip to Main Content

I am trying to understand –become in order to use ansible to do some local task on my centos. I tried several ansible modules (copy, unarchive) with become that each result with diffetent kind of errors.

Platform used: centos 7

Ansible (installed in a python 3 virtual env) version:

(ansible) [maadam@linux update_centos]$ ansible --version
ansible 2.10.16
  config file = None
  configured module search path = ['/home/maadam/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/maadam/Sources/python/venv/ansible/lib64/python3.6/site-packages/ansible
  executable location = /home/maadam/Sources/python/venv/ansible/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

I tried to reproduice the example provided by @techraf in this issue to test become: Using –become for ansible_connection=local.

I used the same playbook:

---
- hosts: localhost
  gather_facts: no
  connection: local
  tasks:
    - command: whoami
      register: whoami
    - debug:
        var: whoami.stdout

So I hope the same result as this:

(ansible) [maadam@linux update_centos]$ sudo whoami
root

Whithout become:

ansible) [maadam@linux update_centos]$ ansible-playbook playbook.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'

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

TASK [command] *****************************************************************************************
changed: [localhost]

TASK [debug] *******************************************************************************************
ok: [localhost] => {
    "whoami.stdout": "maadam"
}

PLAY RECAP *********************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

With become I have this error:

(ansible) [maadam@linux update_centos]$ ansible-playbook playbook.yml --become
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'

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

TASK [command] *****************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/var/tmp/sclPip796: line 8: -H: command not foundn", "module_stdout": "", "msg": "MODULE FAILUREnSee stdout/stderr for the exact error", "rc": 127}

PLAY RECAP *********************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

So I don’t understand what I am missing with become.

Thanks for your helps

2

Answers


  1. Chosen as BEST ANSWER

    I don't know if I handle this correctly but if I run my playbook as root, I have no error:

    (ansible) [maadam@linux update_centos]$ sudo ansible-playbook playbook.yml
    [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    PLAY [localhost] **************************************************************************************************************************************************************************************************
    
    TASK [command] ****************************************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [debug] ******************************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "whoami.stdout": "root"
    }
    
    PLAY RECAP ********************************************************************************************************************************************************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

    Not sure it is the right way to doing things in local with ansible. Sure if you are already root, no need for privilege escalation.


  2. in ansible.cfg file check for the become_method. you can use "sudo su -".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search