我想运行一个角色,但在我想检查程序是否已经安装之前。
我猜我应该使用模块“服务”?在Ubuntu中,服务指向 [? ] arkeia
我试过了:
tasks:
- name: check arkeia
service: name=arkeia state=started
register: result
roles:
- { role: arkeia, when: result==true }
否则,如果无法检查程序是否已安装,我会检查程序文件夹是否存在,然后启动该角色。
编辑:
感谢您的快速回答!
我认为我的缺点是“when:”命令。 但是如何检查结果是否为假?
我收到以下错误:
> PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [managed-centos]
ok: [managed-ubuntu]
ok: [ansible]
TASK: [arkeia | check arkeia] *************************************************
ok: [ansible]
ok: [managed-ubuntu]
ok: [managed-centos]
TASK: [arkeia | create directory] *********************************************
skipping: [managed-centos]
skipping: [managed-ubuntu]
changed: [ansible]
TASK: [arkeia | download sources debian] **************************************
skipping: [managed-centos]
skipping: [managed-ubuntu]
changed: [ansible]
TASK: [arkeia | download sources cent_os] *************************************
skipping: [ansible]
skipping: [managed-ubuntu]
fatal: [managed-centos] => error while evaluating conditional: ansible_os_family == "RedHat" and p.stat.exist == false
TASK: [arkeia | install Arkeia debian] ****************************************
fatal: [ansible] => error while evaluating conditional: ansible_os_family == "Debian" and p.stat.exist == false
fatal: [managed-ubuntu] => error while evaluating conditional: ansible_os_family == "Debian" and p.stat.exist == false
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/home/vagrant/arkeia.retry
ansible : ok=4 changed=2 unreachable=1 failed=0
managed-centos : ok=2 changed=0 unreachable=1 failed=0
managed-ubuntu : ok=2 changed=0 unreachable=1 failed=0
这是我的Playbook:
- name: check arkeia
stat: path=/etc/init.d/arkeia
register: p
- name: create directory
file: state=directory path={{ install_directory }}
when: p.stat.exists == False
- name: download sources debian
get_url: url={{ arkeia_deb_url }} dest={{ install_directory }}/{{ deb_filename }}
when: ansible_os_family == "Debian" and p.stat.exists == false
- name: download sources cent_os
get_url: url={{ arkeia_centos_url }} dest={{ install_directory }}/{{ centos_filename }}
when: ansible_os_family == "RedHat" and p.stat.exist == false
- name: install Arkeia debian
sudo: yes
apt: deb={{ install_directory }}/{{ deb_filename }}
when: ansible_os_family == "Debian" and p.stat.exist == false
- name: install Arkeia centos
sudo: yes
yum: name={{ install_directory }}/{{ centos_filename }}
when: ansible_os_family == "RedHat" and p.stat.exist == false
调试结果:
TASK: [arkeia | debug msg="{{ p }}"] ******************************************
ok: [ansible] => {
"msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'exists': False}, 'changed': False}"
}
ok: [managed-ubuntu] => {
"msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'exists': False}, 'changed': False}"
}
ok: [managed-centos] => {
"msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'uid': 0, 'exists': True, 'woth': False, 'mtime': 1421377097.0, 'inode': 67318822, 'isgid': False, 'size': 1224, 'wgrp': False, 'isuid': False, 'isreg': True, 'pw_name': 'root', 'gid': 0, 'ischr': False, 'wusr': True, 'xoth': True, 'rusr': True, 'nlink': 1, 'issock': False, 'rgrp': True, 'gr_name': 'root', 'path': '/etc/init.d/arkeia', 'xusr': True, 'atime': 1427813706.6750975, 'md5': '52de32627765e8f6e9ff8c5eee7ba232', 'isdir': False, 'ctime': 1427453340.1586156, 'isblk': False, 'xgrp': True, 'dev': 64769, 'roth': True, 'isfifo': False, 'mode': '0755', 'checksum': 'ddd459110d743402d9e18cfe4b3e032faaf54ba1', 'islnk': False}, 'changed': False}"
}
答案 0 :(得分:1)
service
模块不允许您获取当前状态。如果服务不存在,模块执行将失败。
您可以运行shell: service arkeia status
或shell: ps -eaf | grep -w arkeia
或check for file's existance with stat
你做错的其他事情是错误地使用注册变量。它的值是dict
(或地图),其中包含rc, stdout, stdout_lines, stderr, ...
等关键字。所以例如要使用stat
模块,您可以这样做:
- stat: path=/etc/init.d/arkeia
register: p
- debug: msg="/etc/init.d/arkeia exists and is a file"
when: p.stat.isreg is defined and p.stat.isreg
如果您不知道结果的内容,请debug
并打印出来,以便您知道自己要访问的内容:
- name: check arkeia
service: name=arkeia state=started
register: result
- debug: msg="result={{ result }}"
答案 1 :(得分:0)
解决方案......简单地说:)
- name: create directory
file: state=directory path={{ install_directory }}
when: p.stat.exists == False
- name: download sources debian
get_url: url={{ arkeia_deb_url }} dest={{ install_directory }}/{{ deb_filename }}
when: ansible_os_family == "Debian" and p.stat.**exists** == **f**alse
- name: download sources cent_os
get_url: url={{ arkeia_centos_url }} dest={{ install_directory }}/{{ centos_filename }}
when: ansible_os_family == "RedHat" and p.stat.**exist** == **f**alse
使用上部“F”写入存在且为false,并且它有效...