[root@localhost ~]# rpm -qa|grep httpd
[root@localhost ~]#
[root@afei weixin]# vim playbook.yml
---- hosts: all
vars:
my_condition: True
tasks:- name: Ensure the httpd is installed
yum:
name: httpd
state: present
when: my_condition
[root@afei weixin]# ansible-playbook playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [Ensure the httpd is installed]**************************************************
changed:[192.168.240.134]
PLAY RECAP ****************************************************************************192.168.240.134: ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0[root@localhost ~]# rpm -qa|grep httpd
httpd-tools-2.4.37-21.module_el8.2.0+382+15b0afa8.x86_64
httpd-filesystem-2.4.37-21.module_el8.2.0+382+15b0afa8.noarch
centos-logos-httpd-80.5-2.el8.noarch
httpd-2.4.37-21.module_el8.2.0+382+15b0afa8.x86_64
1.2 根据变量是否赋予值来进行条件判断
---[root@afei weixin]# vim playbook.yml
- hosts: all
tasks:- name: Ensure {{ my_servce }} is installed
yum:
name:{{ my_servce }}
state: present
when: my_servce is defined
[root@afei weixin]# ansible-playbook playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [Ensure {{ my_servce }} is installed]********************************************
skipping:[192.168.240.134]
PLAY RECAP ****************************************************************************192.168.240.134: ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
注:这里当没有定义my_servce变量时,系统跳过不会执行安装
[root@afei weixin]# vim playbook.yml
---- hosts: all
vars:
my_servce:
tasks:- name: Ensure {{ my_servce }} is installed
yum:
name:"{{ my_servce }}"
state: present
when: my_servce is defined
[root@afei weixin]# ansible-playbook playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [Ensure is installed]***********************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError:'NoneType' object is not iterable
fatal:[192.168.240.134]: FAILED!=>{"changed": false,"module_stderr":"Shared connection to 192.168.240.134 closed.\r\n","module_stdout":"Traceback (most recent call last):\r\n File \"/root/.ansible/tmp/ansible-tmp-1599454461.9654365-15873-146357723295744/AnsiballZ_dnf.py\", line 102, in <module>\r\n _ansiballz_main()\r\n File \"/root/.ansible/tmp/ansible-tmp-1599454461.9654365-15873-146357723295744/AnsiballZ_dnf.py\", line 94, in _ansiballz_main\r\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n File \"/root/.ansible/tmp/ansible-tmp-1599454461.9654365-15873-146357723295744/AnsiballZ_dnf.py\", line 40, in invoke_module\r\n runpy.run_module(mod_name='ansible.modules.packaging.os.dnf', init_globals=None, run_name='__main__', alter_sys=True)\r\n File \"/usr/lib64/python3.6/runpy.py\", line 205, in run_module\r\n return _run_module_code(code, init_globals, run_name, mod_spec)\r\n File \"/usr/lib64/python3.6/runpy.py\", line 96, in _run_module_code\r\n mod_name, mod_spec, pkg_name, script_name)\r\n File \"/usr/lib64/python3.6/runpy.py\", line 85, in _run_code\r\n exec(code, run_globals)\r\n File \"/tmp/ansible_dnf_payload_0cbwubys/ansible_dnf_payload.zip/ansible/modules/packaging/os/dnf.py\", line 1318, in <module>\r\n File \"/tmp/ansible_dnf_payload_0cbwubys/ansible_dnf_payload.zip/ansible/modules/packaging/os/dnf.py\", line 1305, in main\r\n File \"/tmp/ansible_dnf_payload_0cbwubys/ansible_dnf_payload.zip/ansible/modules/packaging/os/dnf.py\", line 323, in __init__\r\n File \"/tmp/ansible_dnf_payload_0cbwubys/ansible_dnf_payload.zip/ansible/module_utils/yumdnf.py\", line 85, in __init__\r\nTypeError: 'NoneType' object is not iterable\r\n","msg":"MODULE FAILURE\nSee stdout/stderr for the exact error","rc":1}
PLAY RECAP ****************************************************************************192.168.240.134: ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
注:这里虽然定义了my_servce变量,但是没有赋值,所以依旧不能执行
[root@afei weixin]# vim playbook.yml
---- hosts: all
vars:
my_servce: httpd
tasks:- name: Ensure {{ my_servce }} is installed
yum:
name:"{{ my_servce }}"
state: present
when: my_servce is defined
[root@afei weixin]# ansible-playbook playbook.yml
PLAY [all]****************************************************************************
TASK [Gathering Facts]****************************************************************
ok:[192.168.240.134]
TASK [Ensure httpd is installed]******************************************************
changed:[192.168.240.134]
PLAY RECAP ****************************************************************************192.168.240.134: ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0