Here’s my python script for the playbook but the error says you must supply a password
from ansible import context
from ansible.cli import CLI
from ansible.module_utils.common.collections import ImmutableDict
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.parsing.dataloader import DataLoader
from ansible.inventory.manager import InventoryManager
from ansible.vars.manager import VariableManager
loader = DataLoader()
context.CLIARGS = ImmutableDict(
connection=‘network_cli’,
forks=10,
become=None,
become_method=None,
become_user=None,
check=False,
diff=False,
verbosity=2,
syntax=False,
)
passwords={}
passwords[‘vault_password’] = ‘root’
inventory = InventoryManager(loader=loader, sources=(‘inventory.txt’))
variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))
pbex = PlaybookExecutor(playbooks=[‘main.yml’], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords=passwords)
results = pbex.run()
print(results)
the error is like this
Traceback (most recent call last):
File “/home/root1/Documents/python_ansible/index.py”, line 29, in
results = pbex.run()
File “/usr/lib/python3/dist-packages/ansible/executor/playbook_executor.py”, line 136, in run
all_vars = self._variable_manager.get_vars(play=play)
File “/usr/lib/python3/dist-packages/ansible/vars/manager.py”, line 356, in get_vars
data = preprocess_vars(self._loader.load_from_file(found_file, unsafe=True, cache=False))
File “/usr/lib/python3/dist-packages/ansible/parsing/dataloader.py”, line 94, in load_from_file
(b_file_data, show_content) = self._get_file_contents(file_name)
File “/usr/lib/python3/dist-packages/ansible/parsing/dataloader.py”, line 167, in _get_file_contents
return self._decrypt_if_vault_data(data, b_file_name)
File “/usr/lib/python3/dist-packages/ansible/parsing/dataloader.py”, line 137, in _decrypt_if_vault_data
b_data = self._vault.decrypt(b_vault_data, filename=b_file_name)
File “/usr/lib/python3/dist-packages/ansible/parsing/vault/init.py”, line 637, in decrypt
plaintext, vault_id, vault_secret = self.decrypt_and_get_vault_id(vaulttext, filename=filename, obj=obj)
File “/usr/lib/python3/dist-packages/ansible/parsing/vault/init.py”, line 654, in decrypt_and_get_vault_id
raise AnsibleVaultError(“A vault password must be specified to decrypt data”)
ansible.parsing.vault.AnsibleVaultError: A vault password must be specified to decrypt data