Skip to content

Commit ea09daf

Browse files
authored
Merge pull request #92840 from puicchan/master
Fixed documentation error for >= 2.8 plugin
2 parents 745fc66 + a7cf991 commit ea09daf

File tree

1 file changed

+69
-40
lines changed

1 file changed

+69
-40
lines changed

articles/ansible/ansible-manage-azure-dynamic-inventories.md

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,20 @@ Ansible can be used to pull inventory information from various sources (includin
6666
6767
You can [use tags to organize your Azure resources](https://docs.microsoft.com/azure/azure-resource-manager/resource-group-using-tags#azure-cli) by user-defined categories.
6868
69+
### Using Ansible version < 2.8
6970
Enter the following [az resource tag](/cli/azure/resource?view=azure-cli-latest.md#az-resource-tag) command to tag the virtual machine `ansible-inventory-test-vm1` with the key `nginx`:
7071
7172
```azurecli-interactive
7273
az resource tag --tags nginx --id /subscriptions/<YourAzureSubscriptionID>/resourceGroups/ansible-inventory-test-rg/providers/Microsoft.Compute/virtualMachines/ansible-inventory-test-vm1
7374
```
75+
76+
### Using Ansible version >= 2.8
77+
Enter the following [az resource tag](/cli/azure/resource?view=azure-cli-latest.md#az-resource-tag) command to tag the virtual machine `ansible-inventory-test-vm1` with the key `Ansible=nginx`:
78+
79+
```azurecli-interactive
80+
az resource tag --tags Ansible=nginx --id /subscriptions/<YourAzureSubscriptionID>/resourceGroups/ansible-inventory-test-rg/providers/Microsoft.Compute/virtualMachines/ansible-inventory-test-vm1
81+
```
82+
7483
## Generate a dynamic inventory
7584

7685
Once you have your virtual machines defined (and tagged), it's time to generate the dynamic inventory.
@@ -119,10 +128,14 @@ Starting with Ansible 2.8, Ansible provides an [Azure dynamic-inventory plugin](
119128
1. The inventory plugin requires a configuration file. The configuration file must end in `azure_rm` and have an extension of either `yml` or `yaml`. For this tutorial example, save the following playbook as `myazure_rm.yml`:
120129
121130
```yml
122-
plugin: azure_rm
123-
include_vm_resource_groups:
124-
- ansible-inventory-test-rg
125-
auth_source: auto
131+
plugin: azure_rm
132+
include_vm_resource_groups:
133+
- ansible-inventory-test-rg
134+
auth_source: auto
135+
136+
keyed_groups:
137+
- prefix: tag
138+
key: tags
126139
```
127140
128141
1. Run the following command to ping VMs in the resource group:
@@ -151,33 +164,49 @@ Starting with Ansible 2.8, Ansible provides an [Azure dynamic-inventory plugin](
151164
```
152165
153166
## Enable the VM tag
154-
Once you've set a tag, you need to "enable" that tag. One way to enable a tag is by exporting the tag to an environment variable `AZURE_TAGS` via the `export` command:
155167
156-
```azurecli-interactive
157-
export AZURE_TAGS=nginx
158-
```
168+
### If you're using Ansible < 2.8,
159169
160-
- If you're using Ansible < 2.8, run the following command:
170+
- Once you've set a tag, you need to "enable" that tag. One way to enable a tag is by exporting the tag to an environment variable `AZURE_TAGS` via the `export` command:
171+
172+
```azurecli-interactive
173+
export AZURE_TAGS=nginx
174+
```
175+
176+
- Run the following command:
161177
162178
```bash
163179
ansible -i azure_rm.py ansible-inventory-test-rg -m ping
164180
```
181+
182+
You now see only one virtual machine (the one whose tag matches the value exported into the `AZURE_TAGS` environment variable):
183+
184+
```Output
185+
ansible-inventory-test-vm1 | SUCCESS => {
186+
"changed": false,
187+
"failed": false,
188+
"ping": "pong"
189+
}
190+
```
191+
192+
### If you're using Ansible >= 2.8
193+
194+
- run the this command `ansible-inventory -i myazure_rm.yml --graph` to get the following:
195+
196+
```Output
197+
@all:
198+
|--@tag_Ansible_nginx:
199+
| |--ansible-inventory-test-vm1_9e2f
200+
|--@ungrouped:
201+
| |--ansible-inventory-test-vm2_7ba9
202+
```
165203
166-
- If you're using Ansible >= 2.8, run the following command:
204+
- You can also run the following command to test connection to the Nginx VM:
167205
168206
```bash
169-
ansible all -m ping -i ./myazure_rm.yml
207+
ansible -i ./myazure_rm.yml -m ping tag_Ansible_nginx
170208
```
171209
172-
You now see only one virtual machine (the one whose tag matches the value exported into the `AZURE_TAGS` environment variable):
173-
174-
```Output
175-
ansible-inventory-test-vm1 | SUCCESS => {
176-
"changed": false,
177-
"failed": false,
178-
"ping": "pong"
179-
}
180-
```
181210
182211
## Set up Nginx on the tagged VM
183212
@@ -192,19 +221,19 @@ The purpose of tags is to enable the ability to quickly and easily work with sub
192221
1. Paste the following sample code into the editor:
193222

194223
```yml
195-
---
196-
- name: Install and start Nginx on an Azure virtual machine
197-
hosts: all
198-
become: yes
199-
tasks:
200-
- name: install nginx
201-
apt: pkg=nginx state=installed
202-
notify:
203-
- start nginx
204-
205-
handlers:
206-
- name: start nginx
207-
service: name=nginx state=started
224+
---
225+
- name: Install and start Nginx on an Azure virtual machine
226+
hosts: all
227+
become: yes
228+
tasks:
229+
- name: install nginx
230+
apt: pkg=nginx state=installed
231+
notify:
232+
- start nginx
233+
234+
handlers:
235+
- name: start nginx
236+
service: name=nginx state=started
208237
```
209238
210239
1. Save the file and exit the editor.
@@ -213,15 +242,15 @@ The purpose of tags is to enable the ability to quickly and easily work with sub
213242

214243
- Ansible < 2.8:
215244

216-
```bash
217-
ansible-playbook -i azure_rm.py nginx.yml
218-
```
245+
```bash
246+
ansible-playbook -i azure_rm.py nginx.yml
247+
```
219248

220249
- Ansible >= 2.8:
221250

222-
```bash
223-
ansible-playbook -i ./myazure_rm.yml nginx.yml
224-
```
251+
```bash
252+
ansible-playbook -i ./myazure_rm.yml nginx.yml --limit=tag_Ansible_nginx
253+
```
225254

226255
1. After running the playbook, you see output similar to the following results:
227256

@@ -286,4 +315,4 @@ This section illustrates one technique to test that Nginx is installed on your v
286315
## Next steps
287316

288317
> [!div class="nextstepaction"]
289-
> [Quickstart: Configure Linux virtual machines in Azure using Ansible](/azure/virtual-machines/linux/ansible-create-vm)
318+
> [Quickstart: Configure Linux virtual machines in Azure using Ansible](/azure/virtual-machines/linux/ansible-create-vm)

0 commit comments

Comments
 (0)