msg: " OK: Boot Variance is updated with latest new image {{image[0]}} @ {{inventory_hostname}} " when: - output34.stdout is search("BOOT variable = bootflash:{{image[0]}},1;") - output34.stdout is search("Standby BOOT variable = bootflash:{{image[0]}}n,1;") Do this A...
Another common use case for the "when" condition is to run tasks based on the value of a variable. For instance, you may want to perform different tasks based on the operating system of the target host. You can achieve this by using the "ansible_distribution" variable along with the "wh...
-shell: echo "motd contains the word hi" when: motd_contents.stdout.find('hi') != -1 注册的变量值可以用stdout得到,或者用with_items得到,也可以使用stdout_lines得到,如下所示: -name: registered variable usage as a with_items list hosts: all tasks: -name: retrieve the list of home directo...
- name: Set variable with condition hosts: localhost vars: my_var: "{{ 'value1' if condition else 'value2' }}" tasks: - name: Print variable debug: var: my_var 在上面的示例中,根据条件的结果,变量my_var的值将被设置为value1或value2。可以根据实际需求修改条件和变量的值。
when语句:在tasks中使用,Jinja2的语法格式; 代码语言:javascript 复制 -hosts:allremote_user:roottasks:-name:install nginxpackageyum:name=nginx state=latest-name:start nginx service on CentOS6shell:service nginx startwhen:ansible_distribution=="CentOS"and ansible_distribution_major_version=="6"-name:star...
- { role: ROLE3, VARIABLE: VALUE, ...} - { role: ROLE4, when: CONDITION } 事例: 基于角色的方式安装 nginx 1、创建需要的文件 mkdir ./{nginx,memcached,httpd,mysql}/{files,templates,vars,handlers,meta,default,tasks} -pv 3、写tasks/下的主main.yml ...
- { role: ROLE3, VARIABLE: VALUE, ...} - { role: ROLE4, when: CONDITION } 事例: 基于角色的方式安装 nginx 1、创建需要的文件 mkdir ./{nginx,memcached,httpd,mysql}/{files,templates,vars,handlers,meta,default,tasks} -pv 3、写tasks/下的主main.yml ...
default/:此目录中至少应该有一个名为main.yml的文件,用于设定默认变量; 在playbook中调用角色的方法: –hosts: HOSTS remote_user: USERNAME roles: –ROLE1 –ROLE2 –{ role: ROLE3, VARIABLE: VALUE, …} –{ role: ROLE4, when: CONDITION }...
–{ role: ROLE3, VARIABLE: VALUE, …} –{ role: ROLE4, when: CONDITION } 第一步:创建固定目录结构 ]# mkdir /etc/ansible/roles/nginx/{files,tasks,templates,handlers,vars,default,meta} -pv ]# tree /etc/ansible/roles/nginx/ 第二步:提供各目录下的配置文件 ...
- { role: ROLE3, VARIABLE: VALUE, ...} - { role: ROLE4, when: CONDITION } 事例: 基于角色的方式安装 nginx 1、创建需要的文件 mkdir ./{nginx,memcached,httpd,mysql}/{files,templates,vars,handlers,meta,default,tasks} -pv 3、写tasks/下的主main.yml ...