当由 set_fact 设置时,Ansible 似乎忽略了 ansible_shell_type
Ansible seems to ignore ansible_shell_type when set by set_fact
我有一个剧本,它首先在 vSphere 集群上创建虚拟机。它使用 hosts: localhost
所以 API 被 ansible-运行ner 调用。 VM 的创建工作正常。创建并启动 VM 后,我想在创建的 VM 上调用 powershell 脚本。
此时我运行进入以下问题:
{
"unreachable": true,
"msg": "Failed to create temporary directory. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1641299382.7315738-32-66906511694241 `\" && echo ansible-tmp-1641299382.7315738-32-66906511694241=\"` echo ~/.ansible/tmp/ansible-tmp-1641299382.7315738-32-66906511694241 `\" ), exited with result 1",
"changed": false
}
该剧似乎认为它是在 Linux 机器上执行的。这一点也不奇怪,因为 gather_facts
在剧本中设置为 false
。所以游戏无法知道它是一个 Windows 虚拟机。
我之前尝试通过触发以下任务来缓解该问题,但问题仍然存在。
- name: set shell type
set_fact:
ansible_shell_type: powershell
我之前也试过设置os_family,没有成功。
- name: set os fam
set_fact:
ansible_os_family: 'Windows'
当我之前启动这个任务时,它正确显示“powershell”。
- name: DEBUG ansible_shell_type
debug:
var: ansible_shell_type
我现在有点迷茫,任务好像没有利用ansible_shell_type
。
- name: Windows - Regenerate ssh host keys
ansible.windows.win_powershell:
script: |
del "C:\ProgramData\ssh\ssh_host_*"
ssh-keygen.exe -A
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'NT AUTHORITY\SYSTEM';
$ItemList = Get-ChildItem -Path C:\ProgramData\ssh\ssh_host_* -Recurse;
foreach ($Item in $ItemList) {
$Acl = $null; # Reset the $Acl variable to $null
$Acl = Get-Acl -Path $Item.FullName;
$Acl.SetOwner($Account);
$Acl.SetGroup($Account);
Set-Acl -Path $Item.FullName -AclObject $Acl;
}
delegate_to: "{{ vm_ip_address }}"
之前使用 - meta: reset_connection
也没有任何区别。
我现在有点迷路了。希望你们中的任何人都知道我错过了什么。
感谢 Zeitounator!
修复就像直接向任务添加变量一样简单。以下结果按预期工作:
- name: Windows - Regenerate ssh host keys
ansible.windows.win_powershell:
script: |
del "C:\ProgramData\ssh\ssh_host_*"
ssh-keygen.exe -A
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'NT AUTHORITY\SYSTEM';
$ItemList = Get-ChildItem -Path C:\ProgramData\ssh\ssh_host_* -Recurse;
foreach ($Item in $ItemList) {
$Acl = $null; # Reset the $Acl variable to $null
$Acl = Get-Acl -Path $Item.FullName;
$Acl.SetOwner($Account);
$Acl.SetGroup($Account);
Set-Acl -Path $Item.FullName -AclObject $Acl;
}
delegate_to: "{{ vm_ip_address }}"
vars:
ansible_shell_type: powershell
我有一个剧本,它首先在 vSphere 集群上创建虚拟机。它使用 hosts: localhost
所以 API 被 ansible-运行ner 调用。 VM 的创建工作正常。创建并启动 VM 后,我想在创建的 VM 上调用 powershell 脚本。
此时我运行进入以下问题:
{
"unreachable": true,
"msg": "Failed to create temporary directory. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1641299382.7315738-32-66906511694241 `\" && echo ansible-tmp-1641299382.7315738-32-66906511694241=\"` echo ~/.ansible/tmp/ansible-tmp-1641299382.7315738-32-66906511694241 `\" ), exited with result 1",
"changed": false
}
该剧似乎认为它是在 Linux 机器上执行的。这一点也不奇怪,因为 gather_facts
在剧本中设置为 false
。所以游戏无法知道它是一个 Windows 虚拟机。
我之前尝试通过触发以下任务来缓解该问题,但问题仍然存在。
- name: set shell type
set_fact:
ansible_shell_type: powershell
我之前也试过设置os_family,没有成功。
- name: set os fam
set_fact:
ansible_os_family: 'Windows'
当我之前启动这个任务时,它正确显示“powershell”。
- name: DEBUG ansible_shell_type
debug:
var: ansible_shell_type
我现在有点迷茫,任务好像没有利用ansible_shell_type
。
- name: Windows - Regenerate ssh host keys
ansible.windows.win_powershell:
script: |
del "C:\ProgramData\ssh\ssh_host_*"
ssh-keygen.exe -A
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'NT AUTHORITY\SYSTEM';
$ItemList = Get-ChildItem -Path C:\ProgramData\ssh\ssh_host_* -Recurse;
foreach ($Item in $ItemList) {
$Acl = $null; # Reset the $Acl variable to $null
$Acl = Get-Acl -Path $Item.FullName;
$Acl.SetOwner($Account);
$Acl.SetGroup($Account);
Set-Acl -Path $Item.FullName -AclObject $Acl;
}
delegate_to: "{{ vm_ip_address }}"
之前使用 - meta: reset_connection
也没有任何区别。
我现在有点迷路了。希望你们中的任何人都知道我错过了什么。
感谢 Zeitounator!
修复就像直接向任务添加变量一样简单。以下结果按预期工作:
- name: Windows - Regenerate ssh host keys
ansible.windows.win_powershell:
script: |
del "C:\ProgramData\ssh\ssh_host_*"
ssh-keygen.exe -A
$Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList 'NT AUTHORITY\SYSTEM';
$ItemList = Get-ChildItem -Path C:\ProgramData\ssh\ssh_host_* -Recurse;
foreach ($Item in $ItemList) {
$Acl = $null; # Reset the $Acl variable to $null
$Acl = Get-Acl -Path $Item.FullName;
$Acl.SetOwner($Account);
$Acl.SetGroup($Account);
Set-Acl -Path $Item.FullName -AclObject $Acl;
}
delegate_to: "{{ vm_ip_address }}"
vars:
ansible_shell_type: powershell