向大家问好,我最近开始搞乱 Ansible(特别是 Ansible Tower)。我在剧本中使用安全值时遇到了问题,更准确地说,我不明白如何正确使用它。
与 Chef-Infra 相比,您可以使用它data_bags来存储您的安全凭证。您创建一个数据包:
knife data bag create testDataBag
Run Code Online (Sandbox Code Playgroud)
您将为数据包项目创建一个 json 文件:
{
"id": "preproduction",
"user": "user1",
"password": "this-is-a-password"
}
Run Code Online (Sandbox Code Playgroud)
将其上传到 Chef 服务器,同时使用秘密文件(目标服务器存在)对其进行加密:
knife data bag from file testDataBag .\testDataBag\preproduction.json --secret-file .\secret-file
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的食谱中使用它:
userinfo = data_bag_item('testDataBag', preproduction)
userinfo['user'] # "user1"
userinfo['password'] # "this-is-a-password"
Run Code Online (Sandbox Code Playgroud)
示例用例 - 为 Linux 用户配置密码。
userinfo = data_bag_item('testDataBag', preproduction)
user "#{userinfo['user']}" do
comment 'A random user'
home "/home/#{userinfo['user']}"
shell '/bin/bash'
password "userinfo['password']"
end
Run Code Online (Sandbox Code Playgroud)
我知道这是很多信息,但我只是想展示我如何习惯使用安全凭证。回到 Ansible,我了解到有一个ansible-vault工具可以用来加密稍后可以在剧本中使用的变量文件。遗憾的是,我见过的唯一示例(或者也许我只是没有注意到)仅包括从命令行运行剧本,这不是我所做的事情。
我的 GIT 存储库中有一个剧本,它连接到我的 Ansible Tower …
出于自动化测试的目的,我想在执行 ansible-playbook 时禁用保管库,以便在测试中覆盖加密变量。
我见过--ask-vault-pass但没有看到相反的即--no-vault-pass或类似的东西。
ansible设置没有指定环境变量来执行此操作。
我正在尝试读取包含该标签的 YAML 文件!vault。我收到错误:
无法确定标签“!vault”的构造函数
在阅读了几篇博客后,我了解到我需要指定一些构造函数来解决此问题,但我不清楚如何做到这一点。
import yaml
from yaml.loader import SafeLoader
with open('test.yml' ) as stream:
try:
inventory_info = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
User = inventory_info['all']['children']['linux']['vars']['user']
key = inventory_info['all']['children']['linux']['vars']['key_file']
Run Code Online (Sandbox Code Playgroud)
我正在使用的 YAML 文件:
all:
children:
rhel:
hosts: 172.18.144.98
centos:
hosts: 172.18.144.98
linux:
children:
rhel:
centos:
vars:
user: "o9ansibleuser"
key_file: "test.pem"
ansible_password: !vault |
$ANSIBLE_VAULT;2.1;AES256
3234567899353936376166353
Run Code Online (Sandbox Code Playgroud) 我想自动解密ansible保险库,而不必从命令行指定保险库位置或密码.
主要是,我正在寻找一种解决方案,其工作方式类似于工作方式~/.netrc.当您在其中放置凭据时,工具(如curl,wget,...)将知道加载它们.
如果你不这样做,执行就会有.
这非常重要,因为我需要能够自动执行这些ansible playbooks,因此提示输入密码显然不是一种选择.
另外,我不想--vault-password-file为每个剧本添加参数.有任何想法吗?
在使用 Ansible 时,我尝试使用保管的 vars 文件来存储私有变量,然后在另一个 vars 文件中以相同的角色使用这些变量。(这个想法来自“Vault Pseudo leaf crypto” 。)
例如我有一个标准的 vars 文件roles/myrole/vars/main.yml:
---
my_variable: '{{ my_variable_vaulted }}'
Run Code Online (Sandbox Code Playgroud)
然后是加密的roles/myrole/vars/vaulted_vars.yml:
---
my_variable_vaulted: 'SECRET!'
Run Code Online (Sandbox Code Playgroud)
但是当我运行剧本时,我总是收到“错误!错误!‘my_variable_vaulted’未定义”。
我在没有加密第二个文件的情况下尝试过,以确保这不是加密问题,但我遇到了相同的错误。
我有一个Vagrantfile配置一个带Ansible的Virtualbox VM.Ansible playbook包含Ansible Vault加密变量.我的问题是,虽然我通过了选项,但Vagrant配置不会提示输入密码.
Vagrantfile:
Vagrant.configure(2) do |config|
config.vm.provider "virtualbox" do |vb|
# Build a master VM for this box and clone it for individual VMs
vb.linked_clone = true
end
config.vm.box = "bento/ubuntu-16.04"
config.vm.hostname = "test-vm"
config.vm.provision :ansible do |ansible|
ansible.verbose = true
ansible.playbook = "playbook.yml"
ansible.ask_vault_pass = true
# ansible.raw_arguments = --ask-vault-pass
# ansible.raw_arguments = ["--vault-id", "@prompt"]
# ansible.raw_arguments = ["--vault-id", "dev@prompt"]
end
end
Run Code Online (Sandbox Code Playgroud)
playbook.yml:
---
- name: Test
hosts: all
vars:
foo: !vault |
$ANSIBLE_VAULT;1.1;AES256
65306264626234353434613262613835353463346435343735396138336362643535656233393466 …Run Code Online (Sandbox Code Playgroud) 我正在尝试加密一些密码,但只想加密字符串的一部分而不是整个文件。因此,有一个命令 ansible-vault encrypt_string 可为您提供加密输出,但是当我将它添加到我的 .yaml 剧本时,它无法解密。
用于加密简单密码的命令:
ansible-vault encrypt_string 'Test123!' --name 'ansible_password'
结果:
ansible_password: !vault | $ ANSIBLE_VAULT; 1.1; AES256 30333733643939646130396638646138636338636162316536313236666334656338306634353434 3132326265313639623039653261336265343733383730340a663565323932636138633365386332 36363534326263326633623238653464376637646632363839313464333830363436643561626534 6338613837393539350a383962663766373466376138376666393639373631313861663866333663 6137加密成功
^
长字符串的格式有点笨重
所以我试图把这个值放到我的剧本中:
---
- name: Copy needed files to target machine
hosts: prod
vars:
ansible_user: test_admin
ansible_password: !vault $ANSIBLE_VAULT;1.1;AES256;303337336439396461303966386461386363386361623165363132366663346563383066343534343132326265313639623039653261336265343733383730340a663565323932636138633365386332363635343262633266336232386534643766376466323638393134643338303634366435616265346338613837393539350a3839626637663734663761383766663936393736313138616638663336636137
ansible_connection: winrm
ansible_winrm_transport: credssp
ansible_winrm_server_cert_validation: ignore
tasks:
- name: Copy test
win_copy:
src: /etc/winmachines/hosts
dest: C:\test\
Run Code Online (Sandbox Code Playgroud)
然后我想用命令执行剧本:
ansible-playbook copy.yaml -i hosts.ini
结果:
PLAY [Copy needed files to target machine] ********************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************
fatal: …Run Code Online (Sandbox Code Playgroud) 我正在使用 ansible 2.7.16。
\n\nansible 文档说:
\n\nSingle Encrypted Variable\n\nAs of version 2.3, Ansible can now use a vaulted variable that lives in an otherwise \xe2\x80\x98clear text\xe2\x80\x99 YAML file:\n\nnotsecret: myvalue\nmysecret: !vault |\n $ANSIBLE_VAULT;1.1;AES256\n 66386439653236336462626566653063336164663966303231363934653561363964363833313662\n 6431626536303530376336343832656537303632313433360a626438346336353331386135323734\n 62656361653630373231613662633962316233633936396165386439616533353965373339616234\n 3430613539666330390a313736323265656432366236633330313963326365653937323833366536\n 34623731376664623134383463316265643436343438623266623965636363326136\nother_plain_text: othervalue\nRun Code Online (Sandbox Code Playgroud)\n\n我有以下 .yml 文件:
\n\nuser: dbuser\npass: !vault |\n $ANSIBLE_VAULT;1.1;AES256\n 33633131346338633461336438656463643539396535656432306564636466353338373135346166\n 3838313236383739616239333265323131376231656633350a613333613239646263393330353930\n 31303935646330643831396130343031613063393839353433646338343034386432656435623934\n 6537356530643136310a373835323666393337346562613831613962323261346232323331343631\n 3838\nRun Code Online (Sandbox Code Playgroud)\n\n我想获得解密的文件,然后我尝试了以下命令:
\n\nansible-playbook --vault-password-file pass.txt config.yml
但我收到以下错误:
\n\n [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source\n\n [WARNING]: No inventory was parsed, only implicit …Run Code Online (Sandbox Code Playgroud) 我能够使用另一种机制(Azure管道秘密功能)来加密变量,所以我想在那里保存一个ansible-vault密码(在Azure管道中)并将其作为额外的var传递给playbook执行。
请问可以吗?
我期待什么/如何期待的一个例子是
ansible-playbook --extra-vars "vault-password=${pipelinevariable}"
Run Code Online (Sandbox Code Playgroud) 我有一个非常奇怪的问题无法解决。已经调试了4个小时但是没有任何进展,以前从未遇到过这样的事情。
\n安西布的资料:
\nansible [core 2.11.5]\n config file = /Users/igor/Projects/infrastructure/mint2/ansible.cfg\n configured module search path = ['/Users/igor/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']\n ansible python module location = /Users/igor/.local/share/virtualenvs/mint2-x8LdeqZ_/lib/python3.9/site-packages/ansible\n ansible collection location = /Users/igor/.ansible/collections:/usr/share/ansible/collections\n executable location = /Users/igor/.local/share/virtualenvs/mint2-x8LdeqZ_/bin/ansible\n python version = 3.9.6 (default, Jun 28 2021, 19:24:41) [Clang 12.0.5 (clang-1205.0.22.9)]\n jinja version = 3.0.1\n libyaml = False\nRun Code Online (Sandbox Code Playgroud)\n这是一个示例剧本:
\n---\n- hosts: somehost\n gather_facts: yes\n vars_files:\n - host_vars/somehost/common/common.yml\n - host_vars/somehost/backup/backup.yml\n pre_tasks:\n - name: Prepare | Remove AppArmor\n apt:\n name: apparmor\n state: absent\n \n - name: …Run Code Online (Sandbox Code Playgroud)