有时候我们可能不想在某一台远程主机上安装某一个特定版本的软件包,因为这个主机的系统可能和其他的系统不一样
有时候我们系统执行一些clean up操作当我们发现系统满了的情况
好了,我们看一下例子
:关闭操作系统为debian的远程主机
tasks:
– name: “shutdown Debian flavoer system”
command: /sbin/shutdown -t now
when :ansible_os_family==”Debian”
:关闭Centos6 或者Centos7的远程主机
tasks:
– name: “Shut down Centos6 and Centos7 system”
command: /sibn/shutdown -t now
when: ansible_distribution==”Centos” and
(ansible_distribution_major_version ==”6″ or ansible_distribution_major_version == “7”)
:根据上一条命令的返回结果来决定执行的具体命令
tasks:
– command: /bin/false
register: result
ignore_errors: True
– command: /bin/something
when: result|failed
– command: /bin/something_else
when:reuslt|success
– command: /bin/still/something_else
when: result|skipped
我们可以通过如下命令查看哪些变量是可以直接使用的:
ansible hostname.example.com -m setup
:String类型转换为int
tasks:
– shell: echo “only on Red Hat 6, derivatives, and later”
when: ansible_os_family == “Redhat” and ansible_lsb.major_release|int >= 6
我们也可以自己定义一些变量(true|false),然后根据这些变量的值来决定执行什么样的命令
例如:
vars:
epic: true
tasks:
– shell: echo “This certainly is epic!”
when: epic
当然变量没有定义我们也是可以判断的
tasks:
– shell: echo “I’ve got ‘{{foo}}’ and am not afraid to use it!”
when: fo is defined
– fail: msg=”Bailing out.this play requeires’bar'”
when: bar is not defined
另外一个例子
tasks:
– command: echo {{item}}
with_items: [0,2,4,6,8,10]
when: item >5
– hosts: webservers
roles :
-{role: debian_stock_config, when : ansible_os_family==’Debian’}
看一个apache的例子
—
– host: all
remote_user: root
var_files:
– “vars/common.yml”
– [“vars/{{ansible_os_family}}.yml”,”vars/os_defaults.yml”]
tasks:
– name: make sure apache is running
service: name={{apche}} state=running
根据不同的变量来引入不同的配置文件:
– name: template a file
template: src={{ item }} dest=/etc/myapp/foo.conf
with_first_found:
– files:
– {{ ansible_distribution }}.conf
– default.conf
paths:
– search_location_one/somedir/
– /opt/other_location/somedir/
欢迎转载,转载请注明出处:http://www.503error.com/
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021