64045


以下のような ansible のレシピを書いたのですが ansible-playbook を実行すると SyntaxError になって失敗します  
(シェルの中身は AWS inspector の推奨対応をコピーしたもので bash に貼り付けると実行できます）

[code]

    shell: |
    for user in `awk -F: '($3 < 1000) {print $1 }' /etc/passwd` ; do
      if [ $user != ""root"" ]; then
        usermod -L $user
        if [ $user != ""sync"" ] && [ $user != ""shutdown"" ] && [ $user != ""halt"" ]; then
          usermod -s /sbin/nologin $user
        fi
      fi
    done
    
[/code]

<https://docs.ansible.com/ansible/2.5/modules/shell_module.html>  
こちらに複数行のシェル書き方があったので真似してみたんですが  
`shell: |`  
はヒアドキュメントというわけではないんでしょうか

エラーメッセージは以下になります

[code]

    FAILED! => {"reason": "Syntax Error while loading YAML.\n  expected <block end>, but found '<scalar>'\n\nThe error appears to be in '/home/ec2-user/git/ansible/roles/inspector/tasks/user.yml': line 17, column 50, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  shell: |\n  for user in `awk -F: '($3 < 1000) {print $1 }' /etc/passwd` ; do\n                                                 ^ here\nThis one looks easy to fix. It seems that there is a value started\nwith a quote, and the YAML parser is expecting to see the line ended\nwith the same kind of quote. For instance:\n\n    when: \"ok\" in result.stdout\n\nCould be written as:\n\n   when: '\"ok\" in result.stdout'\n\nOr equivalently:\n\n   when: \"'ok' in result.stdout\"\n"}
    
[/code]


CC BY-SA 4.0
2020-03-23T09:27:42.017
64042
2020-03-24T03:24:10.317
2020-03-24T02:21:50.267
4236
question
bash ansible
Ansible の shell のヒアドキュメントについて


エラーメッセージがJSON文字列で表現されているので、エスケープを外すと

[code]

    Syntax Error while loading YAML.
      expected <block end>, but found '<scalar>'
    
    The error appears to be in '/home/ec2-user/git/ansible/roles/inspector/tasks/user.yml': line 17, column 50, but may
    be elsewhere in the file depending on the exact syntax problem.
    
    The offending line appears to be:
      shell: |
      for user in `awk -F: '($3 < 1000) {print $1 }' /etc/passwd` ; do
                                                     ^ here
    This one looks easy to fix. It seems that there is a value started
    with a quote, and the YAML parser is expecting to see the line ended
    with the same kind of quote. For instance:
        when: "ok" in result.stdout
    
    Could be written as:
    
       when: '"ok" in result.stdout'
    
    Or equivalently:
    
       when: "'ok' in result.stdout"
    
[/code]

と書かれています。メッセージ通りであれば、

[code]

      shell: |
      for user in `awk -F: '($3 < 1000) {print $1 }' /etc/passwd` ; do
    
[/code]

と、`shell:`行と同じ高さで記述していますでしょうか？

YAML形式はインデントで表現するため、スクリプトコードを一段下げる必要があります。


CC BY-SA 4.0
2020-03-23T10:53:04.810
64045
2020-03-24T03:24:10.317
2020-03-24T03:24:10.317
4236
4236
64042
answer