简体   繁体   中英

Unable to run ansible adhoc commands on vagrant box?

I'm trying to run the ansible ad-hoc commands on vagrant vms, but getting the following error.

vagrant@10.0.0.12 | FAILED! => {
"cache_update_time": 1543885371,
"cache_updated": false,
"changed": false,
"msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-confold\"     install 'httpd'' failed: E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?\n",
"rc": 100,
"stderr": "E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?\n",
"stderr_lines": [
    "E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)",
    "E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?"
],
"stdout": "",
"stdout_lines": []

I was able to ping them fine, and the ping results are below,

vagrant@10.0.0.1 | SUCCESS => {
"changed": false,
"ping": "pong"
}

I searched couple of stack overflow pages and tried to change the permissions and even removed the "/var/lib/dpkg/lock" but nothing worked.

Configuration: Ubuntu 18.0 vms

vagrant version: Vagrant 2.2.2

ansible version: ansible 2.7.4 config file = None configured module search path = ['/Users/arunyantrapragada/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.7/site-packages/ansible executable location = /usr/local/bin/ansible python version = 3.7.1 (default, Nov 6 2018, 18:46:03) [Clang 10.0.0 (clang-1000.11.45.5)]

Command I'm trying to run is:

ansible servers -i inventory -m apt -a "name=httpd state=present"

Can someone please explain me why this is happening and what would be the solution with compromising the security issues and breaks for removing the lock file in "/var/lib/dpkg/lock".

You need to append the --sudo option to your command. Otherwise, ansible will try to run apt-get as the vagrant user, but apt-get (as the error message hints at) needs to be run as root.

ansible servers -i inventory -b -m apt -a "name=httpd state=present"

OR

ansible servers -i inventory -m apt -a "name=httpd state=present" -K ( this will ask for sudo password for host machines.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM