回购中的实际包名称为package-2.6.12-3.el7.x86_64。
目标是使用Ansible安装软件包,以便:
该仓库可以不时更新软件包,但我不知道何时。
我的想法是安装这样的软件包;
- name: Install package
yum:
name: package-2.6
state: present
Run Code Online (Sandbox Code Playgroud)
但是任务失败,因为package-2.6不在仓库中。尽管package该方法很简单,但并不是未来的证明。
更新:
似乎通配符* do有效,例如package-2.6*。
使用 Ansible 2.0.2.0。尝试将两个事实放入 sqlite 数据库中。为了实现它,我正在使用回调插件。这是到目前为止的 python 脚本;
import os
import time
import sqlite3
import json
from ansible.plugins.callback import CallbackBase
dbname = '/etc/ansible/test.db'
TIME_FORMAT='%Y-%m-%d %H:%M:%S'
try:
con = sqlite3.connect(test1)
cur = con.cursor()
except:
pass
def log(host, data):
if type(data) == dict:
invocation = data.pop('invocation', None)
if invocation.get('module_name', None) != 'setup':
return
facts = data.get('ansible_facts', None)
now = time.strftime(TIME_FORMAT, time.localtime())
try:
# `host` is a unique index
cur.execute("REPLACE INTO test2 (now, host, serial) VALUES(?,?,?);",
(
now,
facts.get('ansible_hostname', None),
facts.get('ansible_product_serial', None)
))
con.commit() …Run Code Online (Sandbox Code Playgroud) 我的目标是使用 or 运算符对两个变量进行条件检查。有两个变量:
var1: test
var2: another
Run Code Online (Sandbox Code Playgroud)
使用一个检查一个变量有效。
when: "'te' in var1" # works!
Run Code Online (Sandbox Code Playgroud)
但是使用两个变量总是正确的。
when: "'te' in var1 or var2" # True
when: "'xxx' in var1 or var2" # Also true, but I expect false
when: "'xxx' in var1" or "'xxx' in var2" # syntax error
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
很抱歉,如果之前已经有人问过这个问题,但我找不到任何符合我的问题的解决方案。
我正在尝试将必须转换的字符串转换为整数:
- hosts: myhost
vars:
- variable1: 15
- variable2: "15"
- variable3: "{{ variable2 | int }}"
Run Code Online (Sandbox Code Playgroud)
我尝试使用variable2(string) 进行计算,所以我将其转换为 int 中的 int variable3。但失败并显示错误消息
“({{variable3 + 1}})上发生意外的模板类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 int”
因此,我尝试type_debug找出列出的 3 个变量的每种类型。
variable1是intvariable2是ansibleunicodevariable3是unicodeansibleunicode当我unicode已经使用| int.
我应该怎么办?
在 Ubuntu 上,我想通过 Ansible 使用命令“do-release-upgrade”升级操作系统,但它有无法避免的提示。
有没有办法使用 Ansible 来回答运行 Ansible 脚本的用户的提示?
我们有几台服务器无法通过 SSH 访问,但通过 Ansible 可以。
服务器运行在 Ubuntu 16.04 上,所有服务器都需要升级到 Ubuntu 20.04。
这个过程中有很多不可避免的提示。
我配置了几个set_fact任务。然而,对我来说,感觉这并不干燥。
例如,我配置了 2 个不同的任务来根据不同的 when 子句设置事实:
- set_fact:
installed: false
when: "'abc' not in ansible_facts.packages"
- set_fact:
installed: true
when: "'abc' in ansible_facts.packages"
Run Code Online (Sandbox Code Playgroud)
我使用的另一个例子:
- name: set fact for bootstrapper
set_fact:
bootstrapper: true
when: cluster.bootstrapper
- name: set fact for not bootstrapper
set_fact:
bootstrapper: false
when: not cluster.bootstrapper
Run Code Online (Sandbox Code Playgroud)
问:是否有更有效的方法可以在单个任务中设置所有这些事实?
我尝试使用 Ansible 设置 MySQL 数据库,但是在更改初始 root 密码时遇到问题。
- name: Get temporary root password from install log
shell: cat /var/log/mysqld.log | grep "temporary password" | grep -oE '[^ ]+$'
register: tmp_root_password
- name: Set new password from temporary password
shell: 'mysql -e \"ALTER USER root IDENTIFIED BY("{{ mysql_root_password }}");\" --connect-expired-password -uroot -p"{{ tmp_root_password.stdout }}"'
Run Code Online (Sandbox Code Playgroud)
失败并出现以下错误:
fatal: [mysqlhost.mydomain]: FAILED! => {"changed": true, "cmd": "mysql -e \\\"ALTER USER root IDENTIFIED BY(\" MyNewPassword\");\\\" --connect-expired-password -uroot -p\"MyTmpPassword\"", "delta": "0:00:00.003081", "end": "2021-11-28 08:40:52.000198", "msg": "non-zero return …Run Code Online (Sandbox Code Playgroud) 试图减去一个变量的数字,这是 Ansible 中的 int。
var:
number: 30
tasks:
- set_fact: me={{ number -1 }}
- debug: var=me
Run Code Online (Sandbox Code Playgroud)
期待: me = 29
结果:
fatal: [node1]: FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{ number - 1 }}): unsupported operand type(s) for -: 'AnsibleUnicode' and 'int'"}
Run Code Online (Sandbox Code Playgroud)