Jorge Aranda
Jorge Aranda

Reputation: 2080

How do I run a single ansible task with sudo -E?

I know I can run playbooks and tasks with sudo, but is there a way to run sudo with arguments? Specifically, I need to run sudo -E <command> to preserve the environment of the logged-in user.

If this is not possible, are there good workarounds?


Edit

Two clarifications:

  1. I'm trying to run with sudo -E just for some tasks, not the whole playbook
  2. A somewhat convoluted example of what I'm trying to do: I have an ubuntu EC2 instance where the ubuntu user has some hosts configured under ~/.ssh/config. I'd like to use those settings with the ansible git module without necessarily copying them into /root/.ssh, and I'd like to use them to modify a directory not owned by the ubuntu user. For pulling, for instance, one way to do this directly in the shell is to sudo -E git pull. An ideal solution would let me (1) use the ansible git module, (2) allow me to sudo -E for this task, and to sudo without the -E flag for other tasks.

Upvotes: 5

Views: 2658

Answers (2)

august0490
august0490

Reputation: 884

just use become_flags at task level

become_flags: '-i'

Upvotes: 5

Bruce P
Bruce P

Reputation: 20759

You should be able to do this via the sudo_flags parameter in ansible.cfg

Upvotes: 2

Related Questions