Can I use ansible-playbook "--extra-vars" to execute roles conditionally - ansible

I have an Ansible playbook I am working on and I am trying to make the execution a little more dynamic. I have two roles that don't always need to be run in conjunction, sometimes only one of them needs to be run. I have been digging into the Ansible docs, and I am wondering if I can pass --extra-vars parameters to only run a specific role.
Currently, my playbook looks like this:
---
- hosts: default
become: true
roles:
- role: upgrades
when: {{ upgrades_role }}
- role: custom-packages
when: {{ custom_packages_role }}
So, the goal is to be able to run:
ansible-playbook playbook.yml -e "upgrades_role=upgrades"
And this would only run the upgrades role and skip the custom_packages role.
Similarly, if I want to run both roles on the same hosts/system:
ansible-playbook playbook.yml -e "upgrades_role=upgrades custom_packages_role=custom-packages"
This would run both roles.
Based on my understating of Ansible syntax and the --extra-vars, -e parameter, this seems like it should work. I just want to be sure I am doing this the proper way and avoiding anti-patterns.
Ansible Version: 2.14

Let me provide you with a framework to automate the use case that you described:
Pass --extra-vars parameters to only run a specific role
Create the project
shell> ls -1
ansible.cfg
hosts
playbook.yml.j2
roles
setup.yml
shell> cat ansible.cfg
[defaults]
gathering = explicit
collections_path = $HOME/.local/lib/python3.9/site-packages/
inventory = $PWD/hosts
roles_path = $PWD/roles
retry_files_enabled = false
stdout_callback = yaml
shell> cat hosts
localhost
The playbook setup.yml
Gets the list of the roles. Fit the variable my_roles_dir to your needs.
Creates file my_roles_order.yml with the list my_roles_order. The purpose of this file is to create the order of the roles.
Creates file my_roles_enable.yml with the dictionary my_roles_enable. The purpose of this file is to create defaults.
Creates file playbook.yml from the template. Fit the template to your needs.
shell> cat setup.yml
- hosts: localhost
vars:
my_roles_dir: "{{ lookup('config', 'DEFAULT_ROLES_PATH') }}"
tasks:
- set_fact:
my_roles: "{{ my_roles|default([]) +
lookup('pipe', 'ls -1 ' ~ item).splitlines() }}"
loop: "{{ my_roles_dir }}"
- copy:
dest: "{{ playbook_dir }}/my_roles_order.yml"
content: |
my_roles_order:
{{ my_roles|to_nice_yaml|indent(2, true) }}
force: "{{ my_roles_order_force|default(false) }}"
- include_vars: my_roles_order.yml
- copy:
dest: "{{ playbook_dir }}/my_roles_enable.yml"
content: |
my_roles_enable:
{% for role in my_roles %}
{{ role }}: false
{% endfor %}
force: "{{ my_roles_enable_force|default(false) }}"
- include_vars: my_roles_enable.yml
- template:
src: playbook.yml.j2
dest: "{{ playbook_dir }}/playbook.yml"
shell> cat playbook.yml.j2
- hosts: localhost
become: true
vars_files:
- my_roles_enable.yml
roles:
{% for role in my_roles_order %}
- role: {{ role }}
when: {{ role }}_role|default(my_roles_enable.{{ role }})|bool
{% endfor %}
Test the trivial roles
shell> tree roles/
roles/
├── current_packages
│   └── tasks
│   └── main.yml
├── custom_packages
│   └── tasks
│   └── main.yml
├── stable_packages
│   └── tasks
│   └── main.yml
└── upgrades
└── tasks
└── main.yml
8 directories, 4 files
shell> cat roles/*/tasks/main.yml
- debug:
msg: Role current_packages running ...
- debug:
msg: Role custom_packages running ...
- debug:
msg: Role stable_packages running ...
- debug:
msg: Role upgrades running ...
Run the playbook setup.yml
shell> ansible-playbook setup.yml
PLAY [localhost] ****************************************************************************************
TASK [set_fact] *****************************************************************************************
ok: [localhost] => (item=/scratch/tmp7/test-204/roles)
TASK [copy] *********************************************************************************************
changed: [localhost]
TASK [include_vars] *************************************************************************************
ok: [localhost]
TASK [copy] *********************************************************************************************
changed: [localhost]
TASK [include_vars] *************************************************************************************
ok: [localhost]
TASK [template] *****************************************************************************************
changed: [localhost]
PLAY RECAP **********************************************************************************************
localhost: ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
This creates playbook.yml
shell> cat playbook.yml
- hosts: localhost
become: true
vars_files:
- my_roles_enable.yml
roles:
- role: current_packages
when: current_packages_role|default(my_roles_enable.current_packages)|bool
- role: custom_packages
when: custom_packages_role|default(my_roles_enable.custom_packages)|bool
- role: stable_packages
when: stable_packages_role|default(my_roles_enable.stable_packages)|bool
- role: upgrades
when: upgrades_role|default(my_roles_enable.upgrades)|bool
and the files
shell> cat my_roles_order.yml
my_roles_order:
- current_packages
- custom_packages
- stable_packages
- upgrades
shell> cat my_roles_enable.yml
my_roles_enable:
current_packages: false
custom_packages: false
stable_packages: false
upgrades: false
By default, the playbook runs nothing. Here you can "pass --extra-vars parameters to only run a specific role". For example, enable the role upgrades
shell> ansible-playbook playbook.yml -e upgrades_role=true
PLAY [localhost] *****************************************************************************
TASK [current_packages : debug] **************************************************************
skipping: [localhost]
TASK [custom_packages : debug] ***************************************************************
skipping: [localhost]
TASK [stable_packages : debug] ***************************************************************
skipping: [localhost]
TASK [upgrades : debug] **********************************************************************
ok: [localhost] =>
msg: Role upgrades running ...
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
If you want to change the order and/or the enablement defaults of the roles edit the files my_roles_order.yml and my_roles_enable.yml. For example, put the role upgrades in the first place and enable it by default
shell> cat my_roles_order.yml
my_roles_order:
- upgrades
- current_packages
- custom_packages
- stable_packages
shell> cat my_roles_enable.yml
my_roles_enable:
current_packages: false
custom_packages: false
stable_packages: false
upgrades: true
Update the playbook
shell> ansible-playbook setup.yml
Test it
shell> ansible-playbook playbook.yml
PLAY [localhost] *****************************************************************************
TASK [upgrades : debug] **********************************************************************
ok: [localhost] =>
msg: Role upgrades running ...
TASK [current_packages : debug] **************************************************************
skipping: [localhost]
TASK [custom_packages : debug] ***************************************************************
skipping: [localhost]
TASK [stable_packages : debug] ***************************************************************
skipping: [localhost]
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
It's practical to create a file if you want to enable/disable multiple roles on the command line. For example,
shell> cat myroles.yml
upgrades_role: false
stable_packages_role: true
custom_packages_role: true
Use it in the command line
shell> ansible-playbook playbook.yml -e #myroles.yml
PLAY [localhost] *****************************************************************************
TASK [upgrades : debug] **********************************************************************
skipping: [localhost]
TASK [current_packages : debug] **************************************************************
skipping: [localhost]
TASK [custom_packages : debug] ***************************************************************
ok: [localhost] =>
msg: Role custom_packages running ...
TASK [stable_packages : debug] ***************************************************************
ok: [localhost] =>
msg: Role stable_packages running ...
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
Don't display skipped hosts
shell> ANSIBLE_DISPLAY_SKIPPED_HOSTS=false ansible-playbook playbook.yml -e #myroles.yml
PLAY [localhost] *****************************************************************************
TASK [custom_packages : debug] ***************************************************************
ok: [localhost] =>
msg: Role custom_packages running ...
TASK [stable_packages : debug] ***************************************************************
ok: [localhost] =>
msg: Role stable_packages running ...
PLAY RECAP ***********************************************************************************
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0

The most common way will be to use tags; updating the example in the question:
---
- hosts: default
become: true
roles:
- role: upgrades
tags: upgrades
- role: custom-packages
tags: packages
...
So, the goal is to be able to run:
# Execute all the roles
ansible-playbook playbook.yml
# Execute only the upgrade
ansible-playbook playbook.yml -t upgrades
# Execute only the setup of custom packages
ansible-playbook playbook.yml -t packages
Here is the documentation for tags.
Using variables and when is possible, but you'll need to ensure to cast it to bool.

Related

Unable to get the list value returned by the json query from the ansible facts

I am trying to get the size_available value for the /home filesystem from the ansible facts.
I am using the following code after setting gather_facts: True
{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')}}
This way I get something like this [34545646] with msg: from the debug module.
I need to compare this value to a static one and continue or not the playbook but when I try:
{{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}
I get:
"msg": "template error while templating string: expected token 'end of print statement', got '['. String: > {{ansible_facts['mounts']|json_query('[?mount==`/home`].size_available')[0]}}
Even if the type_debug shows me the result should be indeed a list that should be accessible by the [0] extension.
You need to parenthesize the initial expression before attempting to index the result, like this:
{{
(
ansible_facts['mounts'] |
json_query('[?mount==`/home`].size_available')
)[0]
}}
E.g., if I run this playbook on my system:
- hosts: localhost
gather_facts: true
tasks:
- debug:
msg: >-
{{
(
ansible_facts['mounts'] |
json_query('[?mount==`/home`].size_available')
)[0]
}}
I get this output for the debug task:
TASK [debug] ********************************************************************************************
ok: [localhost] => {
"msg": "402658955264"
}
Q: "Get the size_available value for a mount point from the ansible facts."
A: Declare the dictionary below, for example in group_vars
shell> cat group_vars/all/mount_vars.yml
mount_size_available: "{{ ansible_mounts|
items2dict(key_name='mount',
value_name='size_available') }}"
gives, for example
mount_size_available:
/: 8480206848
/boot/efi: 30278656
/export: 12902629376
Then, you can easily reference an available size at a mount point, for example
mount_size_available['/export']: '12902629376'
Example of a project for testing
shell> tree .
.
├── ansible.cfg
├── group_vars
│   └── all
│   └── mount_vars.yml
├── hosts
└── pb.yml
2 directories, 4 files
shell> cat ansible.cfg
[defaults]
gathering = explicit
inventory = $PWD/hosts
remote_tmp = ~/.ansible/tmp
retry_files_enabled = false
stdout_callback = yaml
shell> cat group_vars/all/mount_vars.yml
mount_size_available: "{{ ansible_mounts|
items2dict(key_name='mount',
value_name='size_available') }}"
size_1G: "{{ 1 * 1024 * 1024 * 1024 }}"
size_10G: "{{ 10 * 1024 * 1024 * 1024 }}"
size_100G: "{{ 100 * 1024 * 1024 * 1024 }}"
shell> cat hosts
localhost
shell> cat pb.yml
- hosts: localhost
tasks:
- setup:
gather_subset: mounts
- debug:
var: mount_size_available
- debug:
var: mount_size_available['/export']
- debug:
msg: "Free space at /export is greater than 10G."
when: mount_size_available['/export'] > size_10G|int
- debug:
msg: "Free space at /export is less than 100G."
when: mount_size_available['/export'] < size_100G|int
gives
shell> ansible-playbook pb.yml
PLAY [localhost] *****************************************************************************
TASK [setup] *********************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
mount_size_available:
/: 8479465472
/boot/efi: 30278656
/export: 12901998592
TASK [debug] *********************************************************************************
ok: [localhost] =>
mount_size_available['/export']: '12901998592'
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: Free space at /export is greater than 10G.
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: Free space at /export is less than 100G.
PLAY RECAP ***********************************************************************************
localhost: ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

How Do I Make An Ansible Playbook Skip A Role?

I am trying to skip the upgrade part of my playbook. The key part looks like this:
hosts: linux_group
name: Upgrade the third-party application
roles:
- role: "upgradeEnv"
when: ENV == inventory_hostname
vars:
logdir: "/home/appuser/external/logs"
become_user: "{{ sudoUser }}"
become_method: sudo
become: yes
tags:
- upgrade
And the key part of the role looks like this:
- name: Upgrade database
shell: "upgradeDB.sh {{ env }}"
vars:
DBURL: "{{ user }}#{{ host }}"
no_log: True
register: register_appupgrade
tags:
- upgrade
- fail:
msg: "Upgrade errors:"
when: register_appupgrade.stderr !=""
tags:
- upgrade
I run the playbook with --skip-tags=upgrade but ansible still goes into the role and runs the tasks so I end up with tags: upgrade specified on each task.
The Upgrade database gets skipped but the fail ends the run due to the when condition.
Why is the role being run from the playbook even when the tags are supposed to be skipped?
Why is the fail task not being skipped?
Given the project for testing
shell> tree .
.
├── ansible.cfg
├── hosts
├── pb.yml
└── roles
└── upgradeEnv
└── tasks
└── main.yml
3 directories, 4 files
shell> cat ansible.cfg
[defaults]
inventory = $PWD/hosts
roles_path = $PWD/roles
remote_tmp = ~/.ansible/tmp
retry_files_enabled = false
stdout_callback = yaml
shell> cat hosts
[linux_group]
test_11
test_13
ansible [core 2.14.1]
The tags keyword means the tags are applied to all tasks at the indentation level.
If you apply tags at the play level
shell> cat pb.yml
- hosts: linux_group
roles:
- role: upgradeEnv
when: ENV == inventory_hostname
tags: upgrade
everything will be skipped
shell> ansible-playbook pb.yml --skip-tags=upgrade
PLAY [linux_group] ***************************************************************************
PLAY RECAP ***********************************************************************************
If you apply tags at the role level
shell> cat pb.yml
- hosts: linux_group
roles:
- role: upgradeEnv
when: ENV == inventory_hostname
tags: upgrade
the role will be skipped
shell> ansible-playbook pb.yml --skip-tags=upgrade
PLAY [linux_group] ***************************************************************************
TASK [Gathering Facts] ***********************************************************************
ok: [test_13]
ok: [test_11]
PLAY RECAP ***********************************************************************************
test_11: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
test_13: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=
You'll see the same result if you omit tags at role level and apply tags at each task in the role
shell> cat pb.yml
- hosts: linux_group
roles:
- role: upgradeEnv
when: ENV == inventory_hostname
shell> cat roles/upgradeEnv/tasks/main.yml
- name: Upgrade database
command: "echo {{ env }}"
register: register_appupgrade
tags: upgrade
- debug:
msg: |
register_appupgrade.stdout: {{ register_appupgrade.stdout }}
register_appupgrade.stderr: {{ register_appupgrade.stderr }}
tags: upgrade
- fail:
msg: Upgrade errors
when: register_appupgrade.stderr != ""
tags: upgrade
If you don't skip tags the play works as expected
shell> ansible-playbook pb.yml -e ENV=test_11 -e env=test
PLAY [linux_group] ***************************************************************************
TASK [Gathering Facts] ***********************************************************************
ok: [test_11]
ok: [test_13]
TASK [upgradeEnv : Upgrade database] *********************************************************
skipping: [test_13]
changed: [test_11]
TASK [upgradeEnv : debug] ********************************************************************
skipping: [test_13]
ok: [test_11] =>
msg: |-
register_appupgrade.stdout: test
register_appupgrade.stderr:
TASK [upgradeEnv : fail] *********************************************************************
skipping: [test_11]
skipping: [test_13]
PLAY RECAP ***********************************************************************************
test_11: ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
test_13: ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0

Display Ansible playbook with lookups interpolated

I have an Ansible playbook that looks, in part, like this:
...
environment:
F2B_DB_PURGE_AGE: "{{ lookup('env','F2B_DB_PURGE_AGE') }}"
F2B_LOG_LEVEL: "{{ lookup('env','F2B_LOG_LEVEL') }}"
SSMTP_HOST: "{{ lookup('env','SSMTP_HOST') }}"
SSMTP_PORT: "{{ lookup('env','SSMTP_PORT') }}"
SSMTP_TLS: "{{ lookup('env','SSMTP_TLS') }}"
...
Is there any way to run ansible-playbook so that it will show the results of the YAML file after replacing the lookups with their values? That is, I would like to be able to run something like ansible-playbook file.yaml --dry-run and see on standard output (assuming the environment variables were set appropriately):
...
environment:
F2B_DB_PURGE_AGE: "20"
F2B_LOG_LEVEL: "debug"
SSMTP_HOST: "smtp.example.com"
SSMTP_PORT: "487"
SSMTP_TLS: "true"
...
Set the environment for testing
shell> cat env.sh
#!/usr/bin/bash
export F2B_DB_PURGE_AGE="20"
export F2B_LOG_LEVEL="debug"
export SSMTP_HOST="smtp.example.com"
export SSMTP_PORT="487"
export SSMTP_TLS="true"
shell> source env.sh
Given the inventory
shell> cat hosts
localhost ansible_connection=local
Q: "Run something like ansible-playbook file.yaml --dry-run and see environment"
A: The below playbook does the job
shell> cat file.yml
- hosts: all
vars:
my_environment:
F2B_DB_PURGE_AGE: "{{ lookup('env','F2B_DB_PURGE_AGE') }}"
F2B_LOG_LEVEL: "{{ lookup('env','F2B_LOG_LEVEL') }}"
SSMTP_HOST: "{{ lookup('env','SSMTP_HOST') }}"
SSMTP_PORT: "{{ lookup('env','SSMTP_PORT') }}"
SSMTP_TLS: "{{ lookup('env','SSMTP_TLS') }}"
tasks:
- block:
- debug:
msg: |
my_environment:
{{ my_environment|to_nice_yaml|indent(2) }}
- meta: end_play
when: dry_run|d(false)|bool
- debug:
msg: Continue ...
Set dry_run=true
shell> ansible-playbook file.yml -e dry_run=true
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: |-
my_environment:
F2B_DB_PURGE_AGE: '20'
F2B_LOG_LEVEL: debug
SSMTP_HOST: smtp.example.com
SSMTP_PORT: '487'
SSMTP_TLS: 'true'
TASK [meta] **********************************************************************************
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
By default, the playbook will execute tasks
shell> ansible-playbook file.yml
PLAY [all] ***********************************************************************************
TASK [debug] *********************************************************************************
skipping: [localhost]
TASK [meta] **********************************************************************************
skipping: [localhost]
TASK [debug] *********************************************************************************
ok: [localhost] =>
msg: Continue ...
PLAY RECAP ***********************************************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Optionally, let the playbook gather facts and use the dictionary ansible_env. Use the filer ansible.utils.keep_keys to select your variables
- hosts: all
gather_facts: true
vars:
my_environment_vars:
- F2B_DB_PURGE_AGE
- F2B_LOG_LEVEL
- SSMTP_HOST
- SSMTP_PORT
- SSMTP_TLS
my_environment: "{{ ansible_env|
ansible.utils.keep_keys(target=my_environment_vars) }}"
tasks:
- block:
- debug:
msg: |
my_environment:
{{ my_environment|to_nice_yaml|indent(2) }}
- meta: end_play
when: dry_run|d(false)|bool
- debug:
msg: Continue ...

Ansible - unable to append correctly using the file plugin

I'm trying to use the Ansible file plugin to append data to a txt file.
This is my playbook:
- name: Setting some variables.
hosts: "{{ target }}"
gather_facts: no
connection: local
tasks:
- name: Delete newDevices file.
file:
path: newDevices.txt
state: absent
run_once: true
- name: Create newDevices file.
file:
path: newDevices.txt
state: touch
run_once: true
- name: Add new devices to file.
delegate_to: 127.0.0.1
lineinfile:
insertafter: EOF
path: newDevices.txt
line: "{{ inventory_hostname }}"
This is my host file:
[lab]
routerA01.mgt.net
routerA02.mgt.net
routerB01.mgt.net
routerB02.mgt.net
This is how I'm running the playbook:
ansible-playbook myplaybook.yml -i hosts --limit "lab[0-1]" -e target=lab
The newDevices.txt file gets deleted and recreated but the contents of the file vary from having 1 hostname or 2 hostnames. If I change the limit in the run command to --limit "lab[0-2]" then the same thing happens. Sometimes 2 hostnames will print and other times all 3 will print. Seems like there will always be a time when the newDevices.txt file will contain one less hostname in it.
Not sure why this is happening. Tried adding a pause after the file is created to maybe give the processing a little extra time, but that didn't help either.
There is a couple of improvements
Put all tasks into a block delegated to localhost. All of them should run only once on the localhost anyway.
Then, there is no point to declare global connection: local
Set hosts: all if you plan to limit the inventory on the command line. In this case, there is no point to put the group into a variable.
Write the file in a loop. This avoids concurrent writing from multiple instances. (What is probably the main reason for your troubles.)
Given the (simplified) inventory
shell> cat hosts
[lab]
A01
A02
B01
B02
The playbook
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- block:
- file:
path: newDevices.txt
state: absent
- file:
path: newDevices.txt
state: touch
- lineinfile:
insertafter: EOF
path: newDevices.txt
line: "{{ item }}"
loop: "{{ ansible_play_hosts }}"
delegate_to: localhost
run_once: true
gives
shell> ansible-playbook pb.yml --limit lab[0:1]
PLAY [all] **************************************************************************************
TASK [file] *************************************************************************************
changed: [A01 -> localhost]
TASK [file] *************************************************************************************
changed: [A01 -> localhost]
TASK [lineinfile] *******************************************************************************
changed: [A01 -> localhost] => (item=A01)
changed: [A01 -> localhost] => (item=A02)
PLAY RECAP **************************************************************************************
A01: ok=3 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> cat newDevices.txt
A01
A02
The above playbook is not idempotent. The file is always recreated. The result is that the files will always keep the hosts from the last play only.
The simplified playbook below is idempotent. The list of the hosts in the file, however, will be cumulative
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- lineinfile:
create: true
insertafter: EOF
path: newDevices.txt
line: "{{ item }}"
loop: "{{ ansible_play_hosts }}"
delegate_to: localhost
run_once: true
gives
shell> ansible-playbook pb.yml --limit lab[0:1]
PLAY [all] **************************************************************************************
TASK [lineinfile] *******************************************************************************
changed: [A01 -> localhost] => (item=A01)
changed: [A01 -> localhost] => (item=A02)
PLAY RECAP **************************************************************************************
A01: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> cat newDevices.txt
A01
A02
shell> ansible-playbook pb.yml --limit lab[0:1]
PLAY [all] **************************************************************************************
TASK [lineinfile] *******************************************************************************
ok: [A01 -> localhost] => (item=A01)
ok: [A01 -> localhost] => (item=A02)
PLAY RECAP **************************************************************************************
A01: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> ansible-playbook pb.yml --limit lab[0:2]
PLAY [all] **************************************************************************************
TASK [lineinfile] *******************************************************************************
ok: [A01 -> localhost] => (item=A01)
ok: [A01 -> localhost] => (item=A02)
changed: [A01 -> localhost] => (item=B01)
PLAY RECAP **************************************************************************************
A01: ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> cat newDevices.txt
A01
A02
B01
shell> ansible-playbook pb.yml --limit lab[0]
PLAY [all] **************************************************************************************
TASK [lineinfile] *******************************************************************************
ok: [A01 -> localhost] => (item=A01)
PLAY RECAP **************************************************************************************
A01: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> cat newDevices.txt
A01
A02
B01
The playbook below is idempotent and the file will keep the list of hosts from the last play only
shell> cat pb.yml
- hosts: all
gather_facts: false
tasks:
- copy:
dest: newDevices.txt
content: |-
{% for host in ansible_play_hosts %}
{{ host }}
{% endfor %}
delegate_to: localhost
run_once: true
Another way to do this would be to have a play targeting localhost itself since the file is to be created on the controller anyway. The file can be created using the "items" of lab inventory group.
Something like below:
- hosts: localhost
connection: local
tasks:
- file:
path: newDevices.txt
state: absent
- lineinfile:
path: newDevices.txt
create: yes
insertafter: EOF
line: "{{ item }}"
loop: "{{ groups['lab'][0:2] }}"
In the above example, the file gets created by lineinfile task itself. You can also remove/change the list slice of [0:2] as per your requirement.
Then run it with:
ansible-playbook myplaybook.yml -i hosts

Ansible hosts to be set to a substring of a passed variable

I have a play like this:
- name: Perform an action on a Runtime
hosts: all
roles:
- role: mule_action_on_Runtime
A variable at invocation (--extra-vars 'mule_runtime=MuleS01-3.7.3-Testing') has a prefix of the host needed (MuleS01). I want to set hosts: MuleS01. How do I do this?
Given that your pattern is always PartIWant-PartIDonCareAbout-AnotherPartAfterOtherDash you could use the split method of Python, then get the first item of the list via the Jinja filter first.
Here is full working playbook as example:
- hosts: local
gather_facts: no
tasks:
- debug:
msg: "{{ mule_runtime.split('-') | first }}"
This yield the recap:
play.yml --extra-vars 'mule_runtime=MuleS01-3.7.3-Testing'
PLAY [local] *******************************************************************
TASK [debug] *******************************************************************
ok: [local] => {
"msg": "MuleS01"
}
PLAY RECAP *********************************************************************
local : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
With the inventory
shell> cat hosts
MuleS01
MuleS02
MuleS03
this playbook
shell> cat pb.yml
- hosts: all
tasks:
- debug:
msg: Set {{ mule_runtime }}
when: mule_runtime.split('-').0 == inventory_hostname
gives
skipping: [MuleS02]
ok: [MuleS01] => {
"msg": "Set MuleS01-3.7.3-Testing"
}
skipping: [MuleS03]

Resources