ansible playbook:无法以 root 身份启动服务
ansible playbook: Cannot launch a service as root
一天中的大部分时间我都在为这个问题苦苦思索,我尝试了所有可能的方法但没有成功,即使在我的系统管理员的帮助下也是如此。 (请注意,我根本不是 ansible 专家,我今天才发现)
上下文:我尝试通过 gitlab 运行 实现 java 服务的持续集成。管道将在推送时 运行 测试、打包 jar,然后 运行 一个古老的剧本来停止现有服务、替换 jar、再次启动服务。我们在 google 云中进行生产,并且运行良好。我正在尝试在此之前添加一个额外的步骤,以便在本地主机上执行相同的操作。
而且我只是不明白为什么 ansible 无法执行 "sudo service XXXX stop|start" 。我得到的只是
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Sorry, try again.\n[sudo via ansible, key=nbjplyhtvodoeqooejtlnhxhqubibbjy] password: \nsudo: 1 incorrect password attempt\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
这是我调用的 gitlab 管道阶段:
indexer-integration:
stage: deploy integration
script:
- ansible-playbook -i ~/git/ansible/inventory deploy_integration.yml --vault-password-file=/home/gitlab-runner/vault.txt
when: on_success
vault.txt 包含保管库加密密码。这是 deploy_integration.yml
---
- name: deploy integration saleindexer
hosts: localhost
gather_facts: no
user: test-ccc #this is the user that I created as a test
connection: local
vars_files:
- /home/gitlab-runner/secret.txt #holds the sudo password
tasks:
- name: Stop indexer
service: name=indexer state=stopped
become: true
become_user: root
- name: Clean JAR
become: true
become_user: root
file:
state: absent
path: '/PATH/indexer-latest.jar'
- name: Copy JAR
become: true
become_user: root
copy:
src: 'target/indexer-latest.jar'
dest: '/PATH/indexer-latest.jar'
- name: Start indexer
service: name=indexer state=started
become: true
become_user: root
用户 'test-ccc' 是我创建的另一个用户(组 root 的一部分,在 sudoer 文件中)以确保它不是与 gitlab-运行ner 用户相关的问题(而且因为这里显然没有人记得那个用户 xD 的 sudo 密码)
我尝试了很多奇怪的东西,包括
shell: echo 'password' | sudo -S service indexer stop
在命令行中有效。但是如果用ansible执行,我得到的只是一条提示信息,要求我输入sudo密码
谢谢
根据评论请求编辑:secret.txt 有:
ansible_become_pass: password
在命令行中使用该用户(su 用户/sudo 服务启动....)并提示输入该密码时,它工作正常。我认为问题是 ansible 总是提示输入密码,或者密码没有正确传递给任务。
sshd_config 有一行 'PermitRootLogin yes'
好的,感谢 techraf 的回复(现已删除),我注意到
行
user: test-ccc
实际上是没有用的,一切仍然是'gitlab-runner'用户运行。所以我:
把我所有的动作写成一个脚本postbuild.sh
将 gitlab-runners 添加到 sudoers 并为该脚本提供无密码
gitlab-runner ALL=(ALL) NOPASSWD:/home/PATH/postbuild.sh
从 ansible 任务中删除了有关传递密码和机密的所有内容,并改为使用:
shell: sudo -S /home/PATH/postbuild.sh
这样就可以了,脚本被执行,服务 stop/start。我将其标记为已回答,即使使用 service: name=indexer state=started 并为用户提供 NOPASSWD:ALL 仍然导致错误(我对问题的评论中的错误)。如果有人可以在评论中阐明这一点....
一天中的大部分时间我都在为这个问题苦苦思索,我尝试了所有可能的方法但没有成功,即使在我的系统管理员的帮助下也是如此。 (请注意,我根本不是 ansible 专家,我今天才发现)
上下文:我尝试通过 gitlab 运行 实现 java 服务的持续集成。管道将在推送时 运行 测试、打包 jar,然后 运行 一个古老的剧本来停止现有服务、替换 jar、再次启动服务。我们在 google 云中进行生产,并且运行良好。我正在尝试在此之前添加一个额外的步骤,以便在本地主机上执行相同的操作。
而且我只是不明白为什么 ansible 无法执行 "sudo service XXXX stop|start" 。我得到的只是
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Sorry, try again.\n[sudo via ansible, key=nbjplyhtvodoeqooejtlnhxhqubibbjy] password: \nsudo: 1 incorrect password attempt\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
这是我调用的 gitlab 管道阶段:
indexer-integration:
stage: deploy integration
script:
- ansible-playbook -i ~/git/ansible/inventory deploy_integration.yml --vault-password-file=/home/gitlab-runner/vault.txt
when: on_success
vault.txt 包含保管库加密密码。这是 deploy_integration.yml
---
- name: deploy integration saleindexer
hosts: localhost
gather_facts: no
user: test-ccc #this is the user that I created as a test
connection: local
vars_files:
- /home/gitlab-runner/secret.txt #holds the sudo password
tasks:
- name: Stop indexer
service: name=indexer state=stopped
become: true
become_user: root
- name: Clean JAR
become: true
become_user: root
file:
state: absent
path: '/PATH/indexer-latest.jar'
- name: Copy JAR
become: true
become_user: root
copy:
src: 'target/indexer-latest.jar'
dest: '/PATH/indexer-latest.jar'
- name: Start indexer
service: name=indexer state=started
become: true
become_user: root
用户 'test-ccc' 是我创建的另一个用户(组 root 的一部分,在 sudoer 文件中)以确保它不是与 gitlab-运行ner 用户相关的问题(而且因为这里显然没有人记得那个用户 xD 的 sudo 密码)
我尝试了很多奇怪的东西,包括
shell: echo 'password' | sudo -S service indexer stop
在命令行中有效。但是如果用ansible执行,我得到的只是一条提示信息,要求我输入sudo密码
谢谢
根据评论请求编辑:secret.txt 有:
ansible_become_pass: password
在命令行中使用该用户(su 用户/sudo 服务启动....)并提示输入该密码时,它工作正常。我认为问题是 ansible 总是提示输入密码,或者密码没有正确传递给任务。 sshd_config 有一行 'PermitRootLogin yes'
好的,感谢 techraf 的回复(现已删除),我注意到
行user: test-ccc
实际上是没有用的,一切仍然是'gitlab-runner'用户运行。所以我:
把我所有的动作写成一个脚本postbuild.sh
将 gitlab-runners 添加到 sudoers 并为该脚本提供无密码
gitlab-runner ALL=(ALL) NOPASSWD:/home/PATH/postbuild.sh
从 ansible 任务中删除了有关传递密码和机密的所有内容,并改为使用:
shell: sudo -S /home/PATH/postbuild.sh
这样就可以了,脚本被执行,服务 stop/start。我将其标记为已回答,即使使用 service: name=indexer state=started 并为用户提供 NOPASSWD:ALL 仍然导致错误(我对问题的评论中的错误)。如果有人可以在评论中阐明这一点....