我陷入了一项任务,我试图使用 with_together 并循环访问多个列表。这是场景:-
我有两组数据:
"node_list": [
"10.2.0.1",
"10.2.0.2",
]
"java_process_list": [
[
"8612",
"8622",
"8623",
"8625"
],
[
"8613",
"8627",
"8628",
"8630"
]
]
Run Code Online (Sandbox Code Playgroud)
现在我希望我的node_list的第一个项目即(10.2.0.1)迭代process_list的第一个列表中的所有4个项目即(8612,8622,8623,8625)等等。
我正在做的是:-
任务1.yml:-
- name: Execute Script to grep IP address of dynamic nodes
command: sh grep_nodes.sh
args:
chdir: "{{ somedir }}"
register: result
- name: set fact
set_fact: dynamic_nodes="{{ item }}"
with_items: "{{ result.stdout_lines}}"
register: items
- name: make a list
set_fact: node_list="{{ items.results | map(attribute='ansible_facts.dynamic_nodes') | list }}"
- debug: var=node_list
- name: Get …Run Code Online (Sandbox Code Playgroud) 我有一个示例角色,我根据操作系统和包管理器安装一些包:
# THIS IS IN tasks/main.yml
- name: Ensure archlinux-keyring is updated
community.general.pacman:
name: archlinux-keyring
state: latest
update_cache: yes
when: ansible_pkg_mgr == "pacman"
Run Code Online (Sandbox Code Playgroud)
如果这个角色作为剧本的一部分运行,Ansible 将收集事实,一切都很好。但是,如果我将其作为独立角色运行:
ansible localhost -m include_role -a name=examplerole
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
localhost | FAILED! => {
"msg": "The conditional check 'ansible_pkg_mgr == \"pacman\"' failed. The error was: error while evaluating conditional (ansible_pkg_mgr == \"pacman\"): 'ansible_pkg_mgr' is undefined
Run Code Online (Sandbox Code Playgroud)
我知道我可以强制 Ansible 在角色中收集这些事实,但是一遍又一遍地收集事实会非常慢(因为这是我在多个角色中反复出现的问题,而且我不能总是将它们包含在剧本中,而且,如果它们在剧本中,则不需要)。
有什么方法可以检查事实是否已经收集,并仅在需要时将它们收集到角色内部?
有人"{{ ansible_hostname }}"在直接调用playbook任务时遇到以下问题吗?
请建议是否有其他方法可以做到这一点,或者如果我在这里做错了什么,我已经尝试了两个lineinfile&replace模块:
---
- name: Playbook to Install CollectD
hosts: servercast01
gather_facts: False
remote_user: root
become: true
tasks:
- name: Replacing hostname entry
lineinfile:
dest: "/tmp/collectd/etc/collectd.conf"
regexp: '#Hostname "myvm01"'
line: 'Hostname "{{ ansible_hostname }}"'
Run Code Online (Sandbox Code Playgroud)
2)带replace模块:
---
- name: Playbook to Install CollectD
hosts: servercast01
gather_facts: False
remote_user: root
become: true
tasks:
- name: Replacing hostname entry
replace:
dest: /tmp/collectd/etc/collectd.conf
regexp: '#Hostname "myvm01"'
replace: 'Hostname "{{ ansible_hostname }}"'
backup: yes
Run Code Online (Sandbox Code Playgroud)
以下是执行剧本时的错误.. …
这是我的2个标签的任务
- name: Set custom iptables rules
iptables_raw:
name: 'iptables_custom_rules'
rules: '{{ iptables_custom_rules }}'
tags: 'commonrules'
- name: Set XXX iptable rules
iptables_raw:
name: 'iptables_wsg_rules'
rules: '{{ iptables_wsg_rules }}'
tags: 'wsgrules'
Run Code Online (Sandbox Code Playgroud)
在iptable.yml文件中,我包含了带标记的角色
- hosts: iptables
roles:
- { role: "Iptables", tags: "commonrules" }
Run Code Online (Sandbox Code Playgroud)
它应该只运行带有commonrules的标签,但是当我运行playbook时它会运行所有任务.
我有许多磁盘,其中有 1 个分区,已用 ext4 预格式化。有时新磁盘是空白的。我需要能够将它们粘贴在我们的任何 ubuntu 服务器中,运行一个 ansible playbook:识别(新)磁盘确保它尚未安装确保我们不会触及根磁盘(如果是新的)创建分区和 ext4 文件系统。为新磁盘创建挂载点,例如“/mnt/{newdiskserial}”,将其存储在变量中,以便我可以扩展 playbook 功能挂载新磁盘
我一直在看这个旧答案:Ansible - using with_items 并且当有条件时 我认为它可能可用或适应性强。但我的ansible woodoo不够强大。我需要一只高水平的手。
无功能代码
最终结果应该是。放入一些新磁盘或带有文件系统的旧磁盘。运行 playbook 并查看它们已安装在 /mnt/serialnumber 中
下面是我构建字符串的剧本。
- name: Construct command for all paths on a particular IP
set_fact:
allcmd: "{{ allcmd | default('') + '\"ls -lrt ' + item.path + ' | tail -57 &&' }}"
loop: "{{ user1[inventory_hostname] }}"
- debug:
msg: "allcmd is:{{ allcmd }}"
Run Code Online (Sandbox Code Playgroud)
输出:
ok: [10.9.9.11] => (item={u'path': u'/tmp/scripts', u'name': u'SCRIPT'}) => {
"ansible_facts": {
"allcmd": "ls -lrt /tmp/scripts | tail -57 &&"
},
"ansible_loop_var": "item",
"changed": false,
"item": {
"name": "SCRIPT",
"path": "/tmp/scripts"
}
}
ok: [10.9.9.11] => (item={u'path': u'/tmp/MON', …Run Code Online (Sandbox Code Playgroud) 因此,我对 Linux 服务器序列号的 ansible“内置”事实进行了高低审视,而且如果没有自定义的 dmidecode ansible 模块,它似乎不可用。真的是这样吗?我觉得奇怪的是,由于有大量内置的 ansible 事实,序列号没有包括在内。
$ /usr/bin/ansible --version
ansible 2.4.2.0
config file = /home/user/.ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules',
u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.6.6 (r266:84292, Aug 9 2016, 06:11:56) [GCC 4.4.7
20120313 (Red Hat 4.4.7-17)]
Run Code Online (Sandbox Code Playgroud) 正如问题所暗示的,我试图评估 Ansible 角色中的一个事实,如果该值大于或等于一个数字并且小于或等于另一个数字;基本上是一个范围。我似乎无法找到如何做到这一点。
这是我的剧本片段的一部分:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ snap_age }} day(s) old and would have been deleted.
when: (old_snap is defined) and (old_snap == true) and (snap_age >= "1")
Run Code Online (Sandbox Code Playgroud)
上面的代码实际上有效,它返回两项,一项是 80 天前的项目,一项是 102 天前的项目。
现在我想要获取年龄在 1 到 100 之间的任何快照。我尝试这样做:
- name: DEBUG Snapshots to be deleted
debug:
msg: The snapshot for {{ inventory_hostname.split("_")[0] }} is {{ snap_age }} day(s) old and would have been deleted.
when: (old_snap …Run Code Online (Sandbox Code Playgroud)