Problem
using lxd connection to provision lxd containers: become directive is ignored
ISSUE TYPE
- Bug Report
COMPONENT NAME
command with become directive
ANSIBLE VERSION
ansible 2.9.7
config file = /home/myhome/.ansible/ansible.cfg
configured module search path = [u'/home/myhome/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 9.2.1 20191008]'
CONFIGURATION
My inventory contains vms and containers: most of them are acceded with ssh connection, and some of containers with lxd connection.
The default config (group_vars:all.yml) is ssh based, and is overriden by lxd config on host_vars and host inventory declarations.
ANSIBLE_CONFIG=/home/myhome/.ansible/ansible.cfg:
[defaults]
roles_path=/home/myhome/Work/repositories/git/cortex/Ansible/roles
yaml_valid_extensions = .yaml, .yml, .inventory
container name:
> lxd-postgreSQL-v12 | RUNNING | 172.20.10.51 (eth0) | | CONTAINER | 0 |
inventory:
all:
children:
databases_postgres:
hosts:
lxd-postgreSQL-v12:
group_vars:
ansible_connection: ssh
ansible_port: 37058
ansible_user: exploitsp
ansible_become: true
host_vars:
ansible_become: false
ansible_connection: lxd
playbook:
---
- hosts: databases
name: "PostgreSQL"
roles:
- app-postgres-install
role/task:
- name: postgres initdb
command: /usr/pgsql-{{ postgres.version }}/bin/pg_ctl -D {{ postgres.database_dir }}/{{ postgres.version }}/data initdb
args:
creates: "{{ postgres.database_dir }}/{{ postgres.version }}/data/base"
become: yes
become_user: postgres
become_method: sudo
OS / ENVIRONMENT
controller: ubuntu 19.10
target: lxd container centos 8 :Release: 8.1.1911
lxd: 4.0.1
STEPS TO REPRODUCE
ANSIBLE_CONFIG=/home/myhome/.ansible/ansible.cfg
ansible-playbook -i inventories/lab/datbases-postgres.inventory -l lxd-postgreSQL-v12 playbooks/app_postgreSQL_deploy.yml --ask-vault -vvv
EXPECTED RESULTS
the task should become ‘postgres’ user become launching command
ACTUAL RESULTS
become is ignored, it stays as root
TASK [app-postgres-install : postgres initdb] ********************************************************************************************************************************************************************************************************************************************************
task path: /home/myhome/Work/repositories/git/cortex/Ansible/roles/app-postgres-install/tasks/postgres-initdb.yml:10
<lxd-postgreSQL-v12> ESTABLISH LXD CONNECTION FOR USER: root
<lxd-postgreSQL-v12> EXEC /bin/sh -c 'echo ~root && sleep 0'
<lxd-postgreSQL-v12> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000 && echo ansible-tmp-1588501978.05-11748-69923050910000="` echo /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000 `" ) && sleep 0'
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/commands/command.py
<lxd-postgreSQL-v12> PUT /home/myhome/.ansible/tmp/ansible-local-9472muw_Ra/tmprand9R TO /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000/AnsiballZ_command.py
<lxd-postgreSQL-v12> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000/ /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000/AnsiballZ_command.py && sleep 0'
<lxd-postgreSQL-v12> EXEC /bin/sh -c '/usr/bin/python3 /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000/AnsiballZ_command.py && sleep 0'
<lxd-postgreSQL-v12> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1588501978.05-11748-69923050910000/ > /dev/null 2>&1 && sleep 0'
fatal: [lxd-postgreSQL-v12]: FAILED! => {
"changed": true,
"cmd": [
"/usr/pgsql-12/bin/pg_ctl",
"-D",
"/app/PSQL/data/pgsql/12/data",
"initdb"
],
"delta": "0:00:00.001490",
"end": "2020-05-03 10:32:58.431009",
"invocation": {
"module_args": {
"_raw_params": "/usr/pgsql-12/bin/pg_ctl -D /app/PSQL/data/pgsql/12/data initdb",
"_uses_shell": false,
"argv": null,
"chdir": null,
"creates": "/app/PSQL/data/pgsql/12/data/base",
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 1,
"start": "2020-05-03 10:32:58.429519",
"stderr": "pg_ctl: cannot be run as root\nPlease log in (using, e.g., \"su\") as the (unprivileged) user that will\nown the server process.",
"stderr_lines": [
"pg_ctl: cannot be run as root",
"Please log in (using, e.g., \"su\") as the (unprivileged) user that will",
"own the server process."
],
"stdout": "",
"stdout_lines": []
}