![Ansible 剧本将消息发布到 kafka 主题中](https://linux22.com/image/775058/Ansible%20%E5%89%A7%E6%9C%AC%E5%B0%86%E6%B6%88%E6%81%AF%E5%8F%91%E5%B8%83%E5%88%B0%20kafka%20%E4%B8%BB%E9%A2%98%E4%B8%AD.png)
Playbook 1:
---
- name: Message into topic
hosts: web1
become: yes
tasks:
- name: post message
expect:
shell: "/usr/local/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic"
responses:
Question:
- "Hi there"
剧本2:
- name: Message into topic
hosts: web1
gather_facts: false
tasks:
- name: post message
shell:
command: /usr/local/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
responses:
(?i)Message: "Hello From Playbook"
尝试了上述两个剧本,但出现了一些错误。无法找到合适的解决方案。[在此处输入图片描述][1]
root@ip-172-31-83-195:/usr/local/kafka# bin/kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
你好 你好
这是我尝试在剧本中运行的命令。如果您有任何可以像上述命令一样带入其他提示的示例(ctrl+c 是退出提示)。请告诉我我们如何在剧本中使用它们。提前致谢!
错误:
Error: For Playbook 1
root@ip-172-31-87-7:~# ansible-playbook Playbook_to_post_message_into_the_topic.yaml
PLAY [Message into topic] ***********************************************************************************************************************************
TASK [post message] *****************************************************************************************************************************************
fatal: [172.31.83.195]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": true, "cmd": "/bin/bash -c \"/usr/local/kafka/bin/kafka-console-producer.sh --broker-list 54.87.252.89:9092 --topic testTopic\"", "delta": "0:00:30.407263", "end": "2022-02-16 08:32:24.990783", "msg": "non-zero return code", "rc": 129, "start": "2022-02-16 08:31:54.583520", "stdout": ">[2022-02-16 08:32:07,214] WARN [Producer clientId=console-producer] Bootstrap broker 54.87.252.89:9092 (id: -1 rack: null) disconnected (org.apache.kafka.clients.NetworkClient)\r\n[2022-02-16 08:32:24,532] WARN [Producer clientId=console-producer] Bootstrap broker 54.87.252.89:9092 (id: -1 rack: null) disconnected (org.apache.kafka.clients.NetworkClient)", "stdout_lines": [">[2022-02-16 08:32:07,214] WARN [Producer clientId=console-producer] Bootstrap broker 54.87.252.89:9092 (id: -1 rack: null) disconnected (org.apache.kafka.clients.NetworkClient)", "[2022-02-16 08:32:24,532] WARN [Producer clientId=console-producer] Bootstrap broker 54.87.252.89:9092 (id: -1 rack: null) disconnected (org.apache.kafka.clients.NetworkClient)"]}
PLAY RECAP **************************************************************************************************************************************************
172.31.83.195 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
错误:适用于 Playbook 2
PLAY [Message into topic] ***********************************************************************************************************************************
TASK [post message] *****************************************************************************************************************************************
fatal: [172.31.83.195]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": false, "msg": "Unsupported parameters for (command) module: command, responses Supported parameters include: _raw_params, _uses_shell, argv, chdir, creates, executable, removes, stdin, stdin_add_newline, strip_empty_ends, warn"}
PLAY RECAP **************************************************************************************************************************************************
172.31.83.195 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0