Ansible blockinfile 模块-插入代码行

Ansible blockinfile 模块-插入代码行

我尝试使用 Ansible 插入代码行(shell 脚本)阻止文件模块。

name: Customized prompt
blockinfile:
path: /etc/profile.d/customized_prompt.sh
create: yes
block: |

#!/bin/bash

# customized prompt normal user and root

if (( "$(id -u)" == "1000" ))
then
    PS1="[\u@\H \w]$ "
elif (( "$(id -u)" == "0" ))
then
    PS1="[\u@\H \w]# "
fi

我有这个 Ansible 错误

[admin@srvansible /etc/ansible]$ ansible-playbook playbook_prompt.yml --ask-become-pass
BECOME password: 
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
could not find expected ':'

The error appears to be in '/etc/ansible/roles/utilities/tasks/main.yml': line 14, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

if (( "$(id -u)" == "1000" ))
then
^ here

我需要帮助:)

谢谢

答案1

YAML 需要正确缩进。

尝试这个:

name: Customized prompt
blockinfile:
  path: /etc/profile.d/customized_prompt.sh
  create: yes
  block: |
    if (( "$(id -u)" == "1000" ))
    then
        PS1="[\u@\H \w]$ "
    elif (( $(id -u)" == "0" ))
        PS1="[\u@\H \w]# "
    fi

相关内容