From 32dc34c70b9f940892e200c2e3b5bd7ccc03b7c6 Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Thu, 22 Feb 2024 12:04:08 +0100 Subject: [PATCH] Make role INJECT_FACTS_AS_VARS friendly Enabled INJECT_FACTS_AS_VARS[1] (which is default behaviour) has very negative impact on Ansible performance [2]. In order to be able to reduce amount of variables and performance penalty, role is being adopted to be used with no injected facts [1] https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html#ansible-facts [2] https://github.com/ansible/ansible/issues/73654 Change-Id: I71a326964be7597ebaad0183c3a774321885c6f5 --- molecule/default/molecule.yml | 18 +++++++++--------- molecule/default/verify.yml | 2 +- tasks/frr_install.yml | 2 +- tasks/main.yml | 2 +- templates/frr.conf.j2 | 2 +- vars/debian.yml | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 8ddd51c..d51363f 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -41,21 +41,21 @@ provisioner: - ip route 10.0.0.0/24 192.168.1.10 frr_bgpd_config: - router bgp 1234 - - "bgp router-id {{ hostvars['primary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }}" - - "neighbor {{ hostvars['secondary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }} remote-as 5678" + - "bgp router-id {{ hostvars['primary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }}" + - "neighbor {{ hostvars['secondary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }} remote-as 5678" - network 192.168.1.0/24 - address-family ipv4 unicast - - " neighbor {{ hostvars['secondary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }} prefix-list pl-allowed-adv out" + - " neighbor {{ hostvars['secondary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }} prefix-list pl-allowed-adv out" - "exit-address-family" - ip prefix-list pl-allowed-adv seq 5 permit 192.168.1.0/24 - ip prefix-list pl-allowed-adv seq 10 deny any secondary: frr_bgpd_config: - router bgp 5678 - - "bgp router-id {{ hostvars['secondary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }}" - - "neighbor {{ hostvars['primary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }} remote-as 1234" + - "bgp router-id {{ hostvars['secondary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }}" + - "neighbor {{ hostvars['primary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }} remote-as 1234" - address-family ipv4 unicast - - " neighbor {{ hostvars['primary']['ansible_' ~ bridge_name | replace('-', '_')]['ipv4']['address'] }} prefix-list pl-allowed-adv in" + - " neighbor {{ hostvars['primary']['ansible_facts'][bridge_name | replace('-', '_')]['ipv4']['address'] }} prefix-list pl-allowed-adv in" - exit-address-family - ip prefix-list pl-allowed-adv seq 5 permit 192.168.1.0/24 - ip prefix-list pl-allowed-adv seq 10 deny any @@ -64,8 +64,8 @@ provisioner: prepare: prepare.yml converge: playbook.yml verify: verify.yml - # config_options: - # defaults: - # inject_facts_as_vars: false + config_options: + defaults: + inject_facts_as_vars: false scenario: name: default diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml index 026989e..1ecf320 100644 --- a/molecule/default/verify.yml +++ b/molecule/default/verify.yml @@ -26,7 +26,7 @@ - name: Ping vlans ip address shell: | set -e - ping -c2 "{{ hostvars[inventory_hostname]['ansible_eth1']['ipv4']['address'] }}" + ping -c2 "{{ hostvars[inventory_hostname]['ansible_facts']['eth1']['ipv4']['address'] }}" changed_when: false register: _ping_vlan until: _ping_vlan is success diff --git a/tasks/frr_install.yml b/tasks/frr_install.yml index b0d57d3..7b3a33e 100644 --- a/tasks/frr_install.yml +++ b/tasks/frr_install.yml @@ -18,7 +18,7 @@ become_user: root block: - name: Apply package management distro specific configuration - include_tasks: "frr_install_{{ ansible_pkg_mgr | lower }}.yml" + include_tasks: "frr_install_{{ ansible_facts['pkg_mgr'] | lower }}.yml" - name: Install required distro packages package: diff --git a/tasks/main.yml b/tasks/main.yml index 6f46a94..314abe0 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -14,7 +14,7 @@ # limitations under the License. - name: Gather variables for each operating system - include_vars: "{{ ansible_os_family | lower }}.yml" + include_vars: "{{ ansible_facts['os_family'] | lower }}.yml" - name: Create required vlan interfaces include_tasks: frr_vlans.yml diff --git a/templates/frr.conf.j2 b/templates/frr.conf.j2 index 3ebed53..04a5623 100644 --- a/templates/frr.conf.j2 +++ b/templates/frr.conf.j2 @@ -3,7 +3,7 @@ ! frr defaults traditional ! -hostname {{ ansible_hostname }} +hostname {{ ansible_facts['hostname'] }} log syslog informational ! ! staticd config diff --git a/vars/debian.yml b/vars/debian.yml index e913c08..989919a 100644 --- a/vars/debian.yml +++ b/vars/debian.yml @@ -25,7 +25,7 @@ frr_repos_keys: state: present frr_repos: - - repo: "deb {{ apt_repo_url | default('https://deb.frrouting.org/frr ' ~ ansible_distribution_release ~ ' frr-stable') }}" + - repo: "deb {{ apt_repo_url | default('https://deb.frrouting.org/frr ' ~ ansible_facts['distribution_release'] ~ ' frr-stable') }}" state: present filename: "frr"