Ansible 条件 - 将注册变量与值进行比较
Ansible Conditional - Compare registered variable to a value
我需要你的帮助,因为我在这里完全不知所措。
我们开始检查 Ansible 作为配置管理器(AWX 项目)。我们目前基于 Chef Infra 配置管理器。
在 Chef 中,当您想控制资源发生时,您可以使用 only_if
或 not_if
filesystem "Test mount" do
fstype "xfs"
mount "/tmp/test"
device "/dev/sda"
action [:create, :enable]
not_if { File.exists?("/tmp/test") }
end
检查 Ansible 后,我发现条件语句等同于 Chef 守卫。
我的问题是:
在我的示例中,我试图查看服务是否存在,如果存在,则执行操作 A,如果不存在,则执行操作 B。
这是我的剧本:
---
- name: Test of powershell
hosts: all
tasks:
- name: test if ABCD service exists
win_shell: |
$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue
if ($a) {
return $(Write-Host -NoNewline $true)
}
return $(Write-Host -NoNewline $false)
register: ABCDExists
- name: print ABCDExists value
debug:
var: ABCDExists
verbosity: 3
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: ABCDExists is "True"
- name: Write service doesn't exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: ABCDExists is "False"
如您所见,最新的条件尝试是 ABCDExists is "True"
\ ABCDExists is "False"
.
如果在我尝试这样做之后:
ABCDExists == False
ABCDExists == "False"
ABCDExists|string() == "False"
我真的在这里不知所措,我无法理解在 Jinja 文档中查找比较运算符等的位置。
如有任何帮助,我们将不胜感激。
更新:
测试后 ABCDExists | bool
我设法跳过了其中一项任务,但不是我预期的任务。
这些是来自 运行 作业的日志(更改后):
ansible-playbook 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Using /etc/ansible/ansible.cfg as config file
SSH password:
host_list declined parsing /tmp/awx_183_ngdrbth5/tmph5taum3q as it did not pass its verify_file() method
Parsed /tmp/awx_183_ngdrbth5/tmph5taum3q inventory source with script plugin
PLAYBOOK: powershell_test.yml **************************************************
1 plays in powershell_test.yml
PLAY [Test of powershell] ******************************************************
TASK [Gathering Facts] *********************************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:2
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/setup.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
ok: [server01]
META: ran handlers
TASK [test if ABCD service exists] *****************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:5
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
changed: [server01] => {
"changed": true,
"cmd": "$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\nif ($a) {\n return $(Write-Host -NoNewline $true)\n}\nreturn $(Write-Host -NoNewline $false)",
"delta": "0:00:00.484379",
"end": "2020-09-01 01:34:23.412182",
"rc": 0,
"start": "2020-09-01 01:34:22.927802",
"stderr": "",
"stderr_lines": [],
"stdout": "True",
"stdout_lines": [
"True"
]
}
TASK [print ABCDExists value] **********************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:14
ok: [server01] => {
"ABCDExists": {
"changed": true,
"cmd": "$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\nif ($a) {\n return $(Write-Host -NoNewline $true)\n}\nreturn $(Write-Host -NoNewline $false)",
"delta": "0:00:00.484379",
"end": "2020-09-01 01:34:23.412182",
"failed": false,
"rc": 0,
"start": "2020-09-01 01:34:22.927802",
"stderr": "",
"stderr_lines": [],
"stdout": "True",
"stdout_lines": [
"True"
]
}
}
TASK [Write service info if service exists] ************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:19
skipping: [server01] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
TASK [Write service doesn't exists] ********************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:26
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
changed: [server01] => {
"changed": true,
"cmd": "New-Item -Path c:\\temp\\ -Name \"text.txt\" -ItemType File -Force\nSet-Content -Path C:\\temp\\test.txt -Value \"Service ABCD doesn't exists\"",
"delta": "0:00:00.531259",
"end": "2020-09-01 01:34:26.380055",
"rc": 0,
"start": "2020-09-01 01:34:25.848796",
"stderr": "",
"stderr_lines": [],
"stdout": "\r\n\r\n Directory: C:\\temp\r\n\r\n\r\nMode LastWriteTime Length Name \r\n---- ------------- ------ ---- \r\n-a---- 9/1/2020 9:34 AM 0 text.txt \r\n\r\n\r\n",
"stdout_lines": [
"",
"",
" Directory: C:\\temp",
"",
"",
"Mode LastWriteTime Length Name ",
"---- ------------- ------ ---- ",
"-a---- 9/1/2020 9:34 AM 0 text.txt ",
"",
""
]
}
META: ran handlers
META: ran handlers
PLAY RECAP *********************************************************************
server01 : ok=4 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
从变量 ABCDExists
的 debug
输出中,我们可以看到一些可能会根据服务的存在而改变的事情。
"failed": false,
"stdout": "True",
我们可以让条件为when:
的任务如下所示来比较"failed"
的布尔值:
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: not ABCDExists.failed | bool
- name: Write service does not exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: ABCDExists.failed | bool
另一种方法是比较"stdout": "True",
中的文本输出:
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: ABCDExists.stdout == 'True'
- name: Write service does not exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: not ABCDExists.stdout == 'True'
有一些方法可以test conditions。
我需要你的帮助,因为我在这里完全不知所措。
我们开始检查 Ansible 作为配置管理器(AWX 项目)。我们目前基于 Chef Infra 配置管理器。
在 Chef 中,当您想控制资源发生时,您可以使用 only_if
或 not_if
filesystem "Test mount" do
fstype "xfs"
mount "/tmp/test"
device "/dev/sda"
action [:create, :enable]
not_if { File.exists?("/tmp/test") }
end
检查 Ansible 后,我发现条件语句等同于 Chef 守卫。
我的问题是: 在我的示例中,我试图查看服务是否存在,如果存在,则执行操作 A,如果不存在,则执行操作 B。 这是我的剧本:
---
- name: Test of powershell
hosts: all
tasks:
- name: test if ABCD service exists
win_shell: |
$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue
if ($a) {
return $(Write-Host -NoNewline $true)
}
return $(Write-Host -NoNewline $false)
register: ABCDExists
- name: print ABCDExists value
debug:
var: ABCDExists
verbosity: 3
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: ABCDExists is "True"
- name: Write service doesn't exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: ABCDExists is "False"
如您所见,最新的条件尝试是 ABCDExists is "True"
\ ABCDExists is "False"
.
如果在我尝试这样做之后:
ABCDExists == False
ABCDExists == "False"
ABCDExists|string() == "False"
我真的在这里不知所措,我无法理解在 Jinja 文档中查找比较运算符等的位置。
如有任何帮助,我们将不胜感激。
更新:
测试后 ABCDExists | bool
我设法跳过了其中一项任务,但不是我预期的任务。
这些是来自 运行 作业的日志(更改后):
ansible-playbook 2.9.10
config file = /etc/ansible/ansible.cfg
configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Using /etc/ansible/ansible.cfg as config file
SSH password:
host_list declined parsing /tmp/awx_183_ngdrbth5/tmph5taum3q as it did not pass its verify_file() method
Parsed /tmp/awx_183_ngdrbth5/tmph5taum3q inventory source with script plugin
PLAYBOOK: powershell_test.yml **************************************************
1 plays in powershell_test.yml
PLAY [Test of powershell] ******************************************************
TASK [Gathering Facts] *********************************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:2
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/setup.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
ok: [server01]
META: ran handlers
TASK [test if ABCD service exists] *****************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:5
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
changed: [server01] => {
"changed": true,
"cmd": "$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\nif ($a) {\n return $(Write-Host -NoNewline $true)\n}\nreturn $(Write-Host -NoNewline $false)",
"delta": "0:00:00.484379",
"end": "2020-09-01 01:34:23.412182",
"rc": 0,
"start": "2020-09-01 01:34:22.927802",
"stderr": "",
"stderr_lines": [],
"stdout": "True",
"stdout_lines": [
"True"
]
}
TASK [print ABCDExists value] **********************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:14
ok: [server01] => {
"ABCDExists": {
"changed": true,
"cmd": "$a = Get-Service -Name ABCD -ErrorAction SilentlyContinue\nif ($a) {\n return $(Write-Host -NoNewline $true)\n}\nreturn $(Write-Host -NoNewline $false)",
"delta": "0:00:00.484379",
"end": "2020-09-01 01:34:23.412182",
"failed": false,
"rc": 0,
"start": "2020-09-01 01:34:22.927802",
"stderr": "",
"stderr_lines": [],
"stdout": "True",
"stdout_lines": [
"True"
]
}
}
TASK [Write service info if service exists] ************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:19
skipping: [server01] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
TASK [Write service doesn't exists] ********************************************
task path: /tmp/awx_183_ngdrbth5/project/powershell_test.yml:26
Using module file /usr/lib/python3.6/site-packages/ansible/modules/windows/win_shell.ps1
Pipelining is enabled.
<server01> ESTABLISH WINRM CONNECTION FOR USER: User@Domain.com on PORT 5985 TO server01
EXEC (via pipeline wrapper)
changed: [server01] => {
"changed": true,
"cmd": "New-Item -Path c:\\temp\\ -Name \"text.txt\" -ItemType File -Force\nSet-Content -Path C:\\temp\\test.txt -Value \"Service ABCD doesn't exists\"",
"delta": "0:00:00.531259",
"end": "2020-09-01 01:34:26.380055",
"rc": 0,
"start": "2020-09-01 01:34:25.848796",
"stderr": "",
"stderr_lines": [],
"stdout": "\r\n\r\n Directory: C:\\temp\r\n\r\n\r\nMode LastWriteTime Length Name \r\n---- ------------- ------ ---- \r\n-a---- 9/1/2020 9:34 AM 0 text.txt \r\n\r\n\r\n",
"stdout_lines": [
"",
"",
" Directory: C:\\temp",
"",
"",
"Mode LastWriteTime Length Name ",
"---- ------------- ------ ---- ",
"-a---- 9/1/2020 9:34 AM 0 text.txt ",
"",
""
]
}
META: ran handlers
META: ran handlers
PLAY RECAP *********************************************************************
server01 : ok=4 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
从变量 ABCDExists
的 debug
输出中,我们可以看到一些可能会根据服务的存在而改变的事情。
"failed": false,
"stdout": "True",
我们可以让条件为when:
的任务如下所示来比较"failed"
的布尔值:
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: not ABCDExists.failed | bool
- name: Write service does not exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: ABCDExists.failed | bool
另一种方法是比较"stdout": "True",
中的文本输出:
- name: Write service info if service exists
win_shell: |
$a = Get-Service -Name ABCD
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "$($a | select-object -property *)"
when: ABCDExists.stdout == 'True'
- name: Write service does not exists
win_shell: |
New-Item -Path c:\temp\ -Name "text.txt" -ItemType File -Force
Set-Content -Path C:\temp\test.txt -Value "Service ABCD doesn't exists"
when: not ABCDExists.stdout == 'True'
有一些方法可以test conditions。