Ansible 简明教程
Ansible - Advanced Execution
在本节中,我们将了解 Ansible 中的高级执行是什么。
In this chapter, we will learn what is advanced execution with Ansible.
How to Limit Execution by Tasks
这是一个非常重要的执行策略,只需执行一个执行,而不需要整个剧本。 For example ,假设您只想要阻止一个服务器(如果出现生产问题),那么在应用补丁后,您只想要启动服务器。
This is a very important execution strategy where one needs to execute only one execution and not the entire playbook. For example, suppose you only want to stop a server (in case a production issue comes) and then post applying a patch you would like to only start the server.
这里在原始剧本中,中止和启动是同一剧本中不同角色的一部分,但可以使用标签来处理它。我们可以为不同的角色(反过来会有不同的任务)提供不同的标签,因此基于执行者提供的标签,将只执行指定的该角色/任务。所以对于上面提供示例,我们可以添加诸如以下之类的标签 -
Here in original playbook stop and start were a part of different roles in the same playbook but this can be handled with the usage of tags. We can provide different tags to different roles (which in turn will have tasks) and hence based on the tags provided by the executor only that specified role/task gets executed. So for the above example provided, we can add tags like the following −
- {role: start-tomcat, tags: ['install']}}
以下命令有助于使用标签 −
The following command helps in using tags −
ansible-playbook -i hosts <your yaml> --tags "install" -vvv
使用以上命令,只将调用 start-tomcat 角色。所提供的标签是区分大小写的。确保向命令传递完全匹配。
With the above command, only the start-tomcat role will be called. The tag provided is case-sensitive. Ensure exact match is being passed to the command.
How to Limit Execution by Hosts
有两种方法来实现特定主机上指定步骤的执行。对于特定角色,可以定义主机的 - 指定该特定角色应该运行在哪个特定主机上。
There are two ways to achieve the execution of specific steps on specific hosts. For a specific role, one defines the hosts - as to which specific hosts that specific role should be run.
Example
- hosts: <A>
environment: "{{your env}}"
pre_tasks:
- debug: msg = "Started deployment.
Current time is {{ansible_date_time.date}} {{ansible_date_time.time}} "
roles:
- {role: <your role>, tags: ['<respective tag>']}
post_tasks:
- debug: msg = "Completed deployment.
Current time is {{ansible_date_time.date}} {{ansible_date_time.time}}"
- hosts: <B>
pre_tasks:
- debug: msg = "started....
Current time is {{ansible_date_time.date}} {{ansible_date_time.time}} "
roles:
- {role: <your role>, tags: ['<respective tag>']}
post_tasks:
- debug: msg = "Completed the task..
Current time is {{ansible_date_time.date}} {{ansible_date_time.time}}"
根据上述示例,根据所提供的,将只调用相应角色。现在,我的主机 A 和 B 在主机(库存文件)中定义。
As per the above example, depending on the hosts provided, the respective roles will only be called. Now my hosts A and B are defined in the hosts (inventory file).
Alternate Solution
一个不同的解决方案可能是使用变量定义剧本的主机,然后通过 --extra-vars 传递一个特定主机地址 −
A different solution might be defining the playbook’s hosts using a variable, then passing in a specific host address via --extra-vars −
# file: user.yml (playbook)
---
- hosts: '{{ target }}'
user: ...
playbook contd….
Running the Playbook
ansible-playbook user.yml --extra-vars "target = "<your host variable>"
如果未定义 {{ target }},该剧本将不执行任何操作。如果需要,还可以传递来自主机文件中的一个组。如果没有提供额外变量,这不造成危害。
If {{ target }} isn’t defined, the playbook does nothing. A group from the hosts file can also be passed through if need be. This does not harm if the extra vars is not provided.