New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Either the YAML parser or shell module insert leading whitespace on non-first lines #12034
Comments
This behavior is not from shell, but from AnsibleModule's Removing P3 tag pending further discussion as to whether this is expected behavior for the
|
I vote to allow multiline. Some languages (like python) cannot be reduced to single line form: - name: Ensure that tokens are NFC-normalized
shell: |
from unicodedata import normalize
if "{{ item }}" != normalize("NFC", "{{ item }}"):
raise RuntimeError("Token {{ item }} isn't in NFC form")
args:
executable: python3
with_items: ["être", "мёд", "mädchen"]
delegate_to: localhost Multiline scripts can keep atomic (perform a single action): - name: Wait until the socket opens
shell: |
import socket, time
from contextlib import closing, suppress
connected = False
with closing(socket.socket()) as sock:
while not connected:
with suppress(ConnectionAbortedError, ConnectionRefusedError):
sock.connect(('localhost', 8080))
connected = True
time.sleep(.5)
args:
executable: python3 Also difference between command and shell notations may be more clearly. Python example: subprocess.run(["ls", "."])
subprocess.run("ls .", shell=True) Dockerfile example: CMD ["ls", "."]
CMD ls . Ansible example (how could it be): - command: ["ls", "."]
- command:
- ls
- .
- shell: ls . |
This breaks here document use in the shell module. For example:
The end-of-document marker will never be hit because a space is inserted before. |
It appears that we already discussed this in a parallel issue/PR and decided the recommended practice should be to use the script module instead. That resulted in us adding a note to use the script module to the docs: #32863 We'll discuss this in the meeting and if consensus hasn't changed, close this issue then. |
Does the script module interpolate jinja as in @rgm3 example above? I tried converting my heredoc script to using the script module, but it doesn't look like it's parsing jinja. If the script module doesn't interpolate jinja, then getting the same functionality requires the template module, the shell module, and the file module (create script, run script, cleanup script). In previous ansible versions, this only required the shell module. |
Just hit this myself after upgrading from ansible 1.9. The proposal of using However it appears there is a really simple workaround. Just put the content in a variable instead. For some reason it seems the leading newline is only added when you assign directly to the - hosts: localhost
tasks:
- shell: |
echo first
echo second
- shell: '{{content}}'
vars:
content: |
echo first
echo second
|
This is resolved in newer Ansible versions. |
|
ISSUE TYPE
Bug Report
COMPONENT NAME
core
ANSIBLE VERSION
2.1
CONFIGURATION
OS / ENVIRONMENT
SUMMARY
Demo:
leading-whitespace-demo.yaml
ansible-playbook -vi localhost, leading-whitespace-demo.yaml
Output:
Note
cmd
is"echo first\n echo second"
: a space has been inserted at the beginning of the second line.Run the same YAML through
yaml.load
, and there's no extra space:Reproduced on Mac OS X and CentOS with Ansible 1.9.2.
STEPS TO REPRODUCE
EXPECTED RESULTS
ACTUAL RESULTS
The text was updated successfully, but these errors were encountered: