如何仅针对euwest区域的local环境运行Ansible Playbook?
问题:如何仅针对euwest区域的local环境运行Ansible Playbook
我在两个区域(euwest、apsouth)下配置了不同环境(local、development、sandbox)的主机,每个主机都定义了Playbook要用到的属性(比如rest_api_port: '8080')。尝试用命令ansible-playbook playbook.yaml --inventory inventory.yaml --limit="euwest:local"执行时,会运行euwest下的所有主机,求正确执行方法。
相关Inventory配置如下:
--- euwest: hosts: local: ansible_connection: local rest_api_host: 127.0.0.1 rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '8080' rest_api_timeout: '60' rest_api_username: admin development: ansible_connection: local rest_api_host: dev.messaging.cloud rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '943' rest_api_timeout: '60' rest_api_username: dev-admin sandbox: ansible_connection: local rest_api_host: sand.messaging.cloud rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '9443' rest_api_timeout: '60' rest_api_username: sand-admin apsouth: hosts: local: ansible_connection: local rest_api_host: 127.0.0.1 rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '8081' rest_api_timeout: '60' rest_api_username: admin development: ansible_connection: local rest_api_host: dev2.messaging.cloud rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '9091' rest_api_timeout: '60' rest_api_username: dev-admin sandbox: ansible_connection: local rest_api_host: sand2.messaging.cloud rest_api_password: "{{ lookup('env', 'REST_API_PASSWORD') }}" rest_api_port: '443' rest_api_timeout: '60' rest_api_username: sand-admin
问题原因
你使用的--limit="euwest:local"语法会被Ansible解析为**“选中euwest组的所有主机,加上所有名为local的主机”**,属于逻辑“或”的关系,因此会执行euwest下的全部主机。
正确执行方法
以下几种方式都可以实现仅运行euwest组下的local主机:
方法1:使用逻辑“与”符号&
用&表示同时满足“属于euwest组”和“主机名为local”两个条件:
ansible-playbook playbook.yaml --inventory inventory.yaml --limit="euwest&local"
方法2:在主机名后加逗号
末尾加逗号可以明确告诉Ansible,local是euwest组下的主机,而非独立组:
ansible-playbook playbook.yaml --inventory inventory.yaml --limit="euwest:local,"
方法3:直接指定组内主机的完整标识
用@组名.主机名的格式精准定位目标:
ansible-playbook playbook.yaml --inventory inventory.yaml --limit="@euwest.local"
内容的提问来源于stack exchange,提问作者firefly2929
相关产品推荐
相关产品推荐

