我在主机清单中有 100 台主机。主机清单如下所示:
[workstations-new]
de001pc[100:201] ansible_connection=ssh ansible_ssh_user=root
Run Code Online (Sandbox Code Playgroud)
当我运行 ansible-playbook 命令时:
ansible-playbook playbooks/linux/uptime.yml -i hosts-inventory --extra-vars "hosts=workstations-new" -k
Run Code Online (Sandbox Code Playgroud)
正在所有主机上运行它,但它不会按升序获取主机。它以非顺序获取主机(103、14、102、100,...)
例子:
TASK: [uptime machine] ********************************************************
changed: [de001pc103]
changed: [de001pc104]
changed: [de001pc102]
changed: [de001pc100]
changed: [de001pc101]
changed: [de001pc107]
changed: [de001pc106]
changed: [de001pc108]
changed: [de001pc109]
changed: [de001pc110]
changed: [de001pc116]
changed: [de001pc112]
................................................
Run Code Online (Sandbox Code Playgroud)
如何运行命令以升序获取主机?
我正在尝试在EC2上配置主机,因此我正在使用Ansible Dynamic Inventory.
我想做的是; 设置每个节点的序列号.
例如:Zookeeper的"myid"配置
Zookeeper需要为每个节点命名为"myid"的序列号; 1为hostA,2为hostB,3为hostC,依此类推.
这是我的剧本中将"myid"文件复制到主机的部分.
- name: Set myid
sudo: yes
template: src=var/lib/zookeeper/myid.j2 dest=/var/lib/zookeeper/myid
Run Code Online (Sandbox Code Playgroud)
而且myid.j2
应该是这样的下面.
{{ serial_number }}
Run Code Online (Sandbox Code Playgroud)
问题是:变量"{{serial_number}}"应该是什么样的?
jinja2 ansible ansible-playbook apache-zookeeper ansible-inventory
我正在尝试编写一个简单的 Ansible Playbook,请查看下面的片段。使用 Ansible 2.4.0.0、Ubuntu 17.04、Python 2.7.13。这是我第一次使用 Ansible 和 Playbooks,所以请不要太苛刻。我究竟做错了什么?
剧本.yml
---
- name: install packages
hosts: dbservers
become: yes
become_method: sudo
become_user: user
tasks:
- name: Update repositories cache and install "python-minimal" package
apt:
name: python-minimal
update_cache: yes
Run Code Online (Sandbox Code Playgroud)
主机文件
---
[dbservers]
db ansible_host=127.0.0.1 ansible_port=22 ansible_user=user ansible_ssh_pass=pass ansible_become_pass=pass ansible_become_user=user
Run Code Online (Sandbox Code Playgroud)
命令: ansible-playbook -i hosts playbook.yml -vvv
上面的命令返回以下错误:
The full traceback is:
File "/tmp/ansible_yozgsn/ansible_module_apt.py", line 287, in <module>
import apt
fatal: [db]: FAILED! => {
"changed": false,
"cmd": "apt-get update",
"failed": true, …
Run Code Online (Sandbox Code Playgroud) 我在 Linux 中有一个符号链接的文件夹,并且想将该文件夹的内容复制到远程计算机上的文件夹。到目前为止,我尝试使用同步命令,因为我正在尝试复制整个树、文件夹和文件。
当我运行同步命令时,它会将文件夹创建为符号链接,并且该文件夹为空。如何在不创建符号链接的情况下复制符号链接的内容?
这可能是一个奇怪的问题,我确信我已经看到了这个问题,但忽略了将其添加为书签。
我的主机存在于多个组中:
[group1]
host1
[group2]
host2
[group3]
host1
host3
Run Code Online (Sandbox Code Playgroud)
我想为这些主机设置组变量文件(这是简单明显的部分):
group_vars/group1.yml
group_vars/group2.yml
group_vars/group3.yml
Run Code Online (Sandbox Code Playgroud)
,并且还使用以下模式为存在的主机分组变量:
group1:&group3
Run Code Online (Sandbox Code Playgroud)
这可以用 group_vars 文件完成吗?例子:
group_vars/group1:&group3.yml
Run Code Online (Sandbox Code Playgroud)
我确信我已经看到过这种样式的文件名,但我不知道如何让它工作。
我想在 Ansible 中设置一个值作为环境变量,然后在另一个剧本中使用它。以下是我的剧本:
获取cmd.yaml
[root@a6296ab33a34 test_code]# vi get-cwd.yaml
- hosts: localhost
connection: local
gather_facts: False
tasks:
#- name: Get directory
# shell: export ACWD="{{ playbook_dir }}"
# when: platform == 'jenkins'
- name: Get CWD
shell: "export ACWD=/test_code_demo"
when: platform != 'jenkins'
- name: DEMO
shell: echo $ACWD
Run Code Online (Sandbox Code Playgroud)
输出
[root@a6296ab33a34 test_code]# vi get-cwd.yaml
[root@a6296ab33a34 test_code]# ansible-playbook get-cwd.yaml --extra-vars="@deploy-vars.yaml" -vv
[WARNING] Ansible is being run in a world writable directory (/test_code), ignoring it as an ansible.cfg source. For more information see …
Run Code Online (Sandbox Code Playgroud) 我希望在多个库存组上运行相同的任务,例如:
[big_group]
greenhat
localhost
redhat
linux
[small_group]
localhost
[redhat_group]
redhat
Run Code Online (Sandbox Code Playgroud)
在我的剧本中,我需要在[small_group]和[redhat_group]上运行一个任务
我的任务如下
[big_group]
greenhat
localhost
redhat
linux
[small_group]
localhost
[redhat_group]
redhat
Run Code Online (Sandbox Code Playgroud)
收到警告作为
[WARNING]: While constructing a mapping from firewall.yml, line 6, column 5, found a duplicate dict key (when). Using last defined value only.
Run Code Online (Sandbox Code Playgroud)
剧本结果:
TASK [Disable HTTPS service from firewalld] **************************************************************************************************************************************************
skipping: [localhost]
skipping: [redhat]
Run Code Online (Sandbox Code Playgroud)
如何指定多个组 when: inventory_hostname in groups[]
谢谢
我仅在主机名具有变量时才尝试执行我的任务servers=admin
。
我正在尝试执行下面的剧本,但遇到错误。
我正在尝试这个,因为它类似于对我有用的东西inventory_hostname in groups['production']
。我想仅当服务器变量包含admin
在其中时才运行该任务。
我的主机文件:
[production]
host1 servers=admin,server1
host2 servers=server2
host3 servers=admin,server3
host4 servers=server4
host5 servers=server5
host6 servers=admin,server6
Run Code Online (Sandbox Code Playgroud)
我的任务:
[production]
host1 servers=admin,server1
host2 servers=server2
host3 servers=admin,server3
host4 servers=server4
host5 servers=server5
host6 servers=admin,server6
Run Code Online (Sandbox Code Playgroud)
host1
我的任务应该只在、host3
和 上运行host6
。
我正在尝试获取主机的主机名和 IP 地址并将它们保存到文件中。
我有这个解决方案;
- name: Create File with hosts and IP address.
when: inventory_hostname in groups['local']
lineinfile:
dest: "{{store_files_path}}/{{ansible_date_time.date}}/{{ansible_date_time.time}}/hosts.txt"
create: yes
line: "{{hostvars[inventory_hostname].ansible_hostname}}:{{hostvars[inventory_hostname].ansible_default_ipv4.address}}"
Run Code Online (Sandbox Code Playgroud)
但问题出在我的主机文件中,我有两个组,local
并且Servers
. 我想要得到Servers
唯一的,而不是local
唯一的组localhost
。
我已经尝试过以下行,但它不起作用,它给了我一个错误。
line: "{{ hostvars[ groups['Servers'][0] ].ansible_hostname }} :{{ hostvars[ groups['Servers'][0] ].ansible_default_ipv4.address }}"
Run Code Online (Sandbox Code Playgroud)
我查了一下,发现是这样的,我该怎么办?
我有一个关于 ansible 中数据操作的具体问题。
在我的清单文件中,我有一个名为 postgresql 的组,如下所示:
[postgresql]
host1 ansible_host=1.1.1.1 postgresql_cluster_port=5432 postgresql_harole=master
host2 ansible_host=2.2.2.2 postgresql_cluster_port=5432 postgresql_harole=slave postgresql_master_ip=1.1.1.1
host3 ansible_host=3.3.3.3 postgresql_cluster_port=5432 postgresql_harole=slave postgresql_master_ip=1.1.1.1
host4 ansible_host=4.4.4.4 postgresql_cluster_port=5432 postgresql_harole=slave postgresql_master_ip=1.1.1.1
Run Code Online (Sandbox Code Playgroud)
在我的剧本中的某个地方,我需要操作和使用过滤器来创建 postgresql_harole=slave 的所有主机的 IP 地址列表,如下所示:
- hosts: postgresql
gather_facts: True
remote_user: root
tasks:
- set_facts:
slave_ip_list: "{{ expressions }}"
Run Code Online (Sandbox Code Playgroud)
我正在拉扯我的头发以获得正确的表达...非常感谢任何帮助!!!!
jinja2 ansible ansible-facts ansible-inventory ansible-filter