@@ -85,6 +85,17 @@ def self.it_should_check_ansible_version
8585 end
8686 end
8787
88+ def self . it_should_check_ansible_core_version
89+ it "execute 'Python ansible-core version check before executing 'ansible-playbook'" do
90+ expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute )
91+ . once . with ( 'python3' , '-c' , "import importlib.metadata; print('ansible-core ' + importlib.metadata.version('ansible-core'))" , { notify : %i[
92+ stdout stderr
93+ ] } )
94+ expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute )
95+ . once . with ( 'ansible-playbook' , any_args )
96+ end
97+ end
98+
8899 def self . it_should_set_arguments_and_environment_variables (
89100 expected_args_count = 5 ,
90101 expected_vars_count = 4 ,
@@ -96,7 +107,7 @@ def self.it_should_set_arguments_and_environment_variables(
96107 expect ( args [ 1 ] ) . to eq ( "--connection=ssh" )
97108 expect ( args [ 2 ] ) . to eq ( "--timeout=30" )
98109
99- inventory_count = args . count { |x | x . match ( /^--inventory=.+$/ ) if x . is_a? ( String ) }
110+ inventory_count = args . count { |x | x . match ( /^--inventory-file =.+$/ ) if x . is_a? ( String ) }
100111 expect ( inventory_count ) . to be > 0
101112
102113 expect ( args [ args . length -2 ] ) . to eq ( "playbook.yml" )
@@ -200,9 +211,9 @@ def self.it_should_create_and_use_generated_inventory(with_user = true)
200211
201212 it "sets as ansible inventory the directory containing the auto-generated inventory file" do
202213 expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
203- inventory_index = args . rindex ( "--inventory=#{ generated_inventory_dir } " )
214+ inventory_index = args . rindex ( "--inventory-file =#{ generated_inventory_dir } " )
204215 expect ( inventory_index ) . to be > 0
205- expect ( find_last_argument_after ( inventory_index , args , /--inventory=\w +/ ) ) . to be ( false )
216+ expect ( find_last_argument_after ( inventory_index , args , /--inventory-file =\w +/ ) ) . to be ( false )
206217 } . and_return ( default_execute_result )
207218 end
208219 end
@@ -286,6 +297,7 @@ def ensure_that_config_is_valid
286297
287298 describe "with default options" do
288299 it_should_check_ansible_version
300+ it_should_check_ansible_core_version
289301 it_should_set_arguments_and_environment_variables
290302 it_should_create_and_use_generated_inventory
291303
@@ -383,6 +395,7 @@ def ensure_that_config_is_valid
383395 end
384396
385397 it_should_check_ansible_version
398+ it_should_check_ansible_core_version
386399 it_should_create_and_use_generated_inventory
387400
388401 it "doesn't warn about compatibility mode auto-detection" do
@@ -621,7 +634,7 @@ def ensure_that_config_is_valid
621634 "-l localhost" ,
622635 "--limit=foo" ,
623636 "--limit=bar" ,
624- "--inventory=/forget/it/my/friend" ,
637+ "--inventory-file =/forget/it/my/friend" ,
625638 "--user=lion" ,
626639 "--new-arg=yeah" ]
627640 end
@@ -639,7 +652,7 @@ def ensure_that_config_is_valid
639652 it "sets raw arguments after arguments related to supported options" do
640653 expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
641654 expect ( args . index ( "--user=lion" ) ) . to be > args . index ( "--user=testuser" )
642- expect ( args . index ( "--inventory=/forget/it/my/friend" ) ) . to be > args . index ( "--inventory=#{ generated_inventory_dir } " )
655+ expect ( args . index ( "--inventory-file =/forget/it/my/friend" ) ) . to be > args . index ( "--inventory-file =#{ generated_inventory_dir } " )
643656 expect ( args . index ( "--limit=bar" ) ) . to be > args . index ( "--limit=all" )
644657 expect ( args . index ( "--skip-tags=ignored" ) ) . to be > args . index ( "--skip-tags=foo,bar" )
645658 } . and_return ( default_execute_result )
@@ -736,8 +749,8 @@ def ensure_that_config_is_valid
736749
737750 it "does not generate the inventory and uses given inventory path instead" do
738751 expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
739- expect ( args ) . to include ( "--inventory=#{ existing_file } " )
740- expect ( args ) . not_to include ( "--inventory=#{ generated_inventory_file } " )
752+ expect ( args ) . to include ( "--inventory-file =#{ existing_file } " )
753+ expect ( args ) . not_to include ( "--inventory-file =#{ generated_inventory_file } " )
741754 expect ( File . exist? ( generated_inventory_file ) ) . to be ( false )
742755 } . and_return ( default_execute_result )
743756 end
@@ -912,7 +925,7 @@ def ensure_that_config_is_valid
912925
913926 it "shows the ansible-playbook command and set verbosity to '-#{ verbose_option } ' level" do
914927 expect ( machine . env . ui ) . to receive ( :detail )
915- . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory=#{ generated_inventory_dir } -#{ verbose_option } playbook.yml" )
928+ . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory-file =#{ generated_inventory_dir } -#{ verbose_option } playbook.yml" )
916929 end
917930 end
918931
@@ -926,7 +939,7 @@ def ensure_that_config_is_valid
926939
927940 it "shows the ansible-playbook command and set verbosity to '-#{ verbose_option } ' level" do
928941 expect ( machine . env . ui ) . to receive ( :detail )
929- . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory=#{ generated_inventory_dir } -#{ verbose_option } playbook.yml" )
942+ . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory-file =#{ generated_inventory_dir } -#{ verbose_option } playbook.yml" )
930943 end
931944 end
932945 end
@@ -941,7 +954,7 @@ def ensure_that_config_is_valid
941954
942955 it "shows the ansible-playbook command and set verbosity to '-v' level" do
943956 expect ( machine . env . ui ) . to receive ( :detail )
944- . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory=#{ generated_inventory_dir } -v playbook.yml" )
957+ . with ( "PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --limit=\" machine1\" --inventory-file =#{ generated_inventory_dir } -v playbook.yml" )
945958 end
946959 end
947960
@@ -1172,7 +1185,7 @@ def ensure_that_config_is_valid
11721185
11731186 it "shows the ansible-playbook command, with additional quotes when required" do
11741187 expect ( machine . env . ui ) . to receive ( :detail )
1175- . with ( %Q(PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/up/to the stars' ANSIBLE_CONFIG='#{ existing_file } ' ANSIBLE_HOST_KEY_CHECKING=true ANSIBLE_SSH_ARGS='-o IdentitiesOnly=yes -o IdentityFile=/my/key1 -o IdentityFile=/my/key2 -o ForwardAgent=yes -o ControlMaster=no -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --ask-sudo-pass --ask-vault-pass --limit="machine*:&vagrant:!that_one" --inventory=#{ generated_inventory_dir } --extra-vars=\\ {\\ "var1\\ ":\\ "string\\ with\\ \\ 'apo\\ $trophe\\ $\\ ',\\ \\ \\ \\ \\ ,\\ \\ \\ \\ "\\ and\\ \\ =\\ ",\\ "var2\\ ":\\ {\\ "x\\ ":42\\ }\\ } --sudo --sudo-user=deployer -vvv --vault-password-file=#{ existing_file } --tags=db,www --skip-tags=foo,bar --start-at-task="joe's awesome task" --why-not --su-user=foot --ask-su-pass --limit=all --private-key=./myself.key --extra-vars='{\" var3\" :\" foo\" }' playbook.yml) )
1188+ . with ( %Q(PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_ROLES_PATH='/up/to the stars' ANSIBLE_CONFIG='#{ existing_file } ' ANSIBLE_HOST_KEY_CHECKING=true ANSIBLE_SSH_ARGS='-o IdentitiesOnly=yes -o IdentityFile=/my/key1 -o IdentityFile=/my/key2 -o ForwardAgent=yes -o ControlMaster=no -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --ask-sudo-pass --ask-vault-pass --limit="machine*:&vagrant:!that_one" --inventory-file =#{ generated_inventory_dir } --extra-vars=\\ {\\ "var1\\ ":\\ "string\\ with\\ \\ 'apo\\ $trophe\\ $\\ ',\\ \\ \\ \\ \\ ,\\ \\ \\ \\ "\\ and\\ \\ =\\ ",\\ "var2\\ ":\\ {\\ "x\\ ":42\\ }\\ } --sudo --sudo-user=deployer -vvv --vault-password-file=#{ existing_file } --tags=db,www --skip-tags=foo,bar --start-at-task="joe's awesome task" --why-not --su-user=foot --ask-su-pass --limit=all --private-key=./myself.key --extra-vars='{\" var3\" :\" foo\" }' playbook.yml) )
11761189 end
11771190 end
11781191
@@ -1262,5 +1275,52 @@ def ensure_that_config_is_valid
12621275 } . and_return ( default_execute_result )
12631276 end
12641277 end
1278+
1279+ describe '#get_inventory_argument' do
1280+ context 'when ansible version is not detected' do
1281+ before do
1282+ config . version = nil
1283+ allow ( subject ) . to receive ( :gather_ansible_version ) . and_return ( "ansible #{ config . version } \n ...\n " )
1284+ allow ( subject ) . to receive ( :gather_ansible_version ) . with ( "ansible-core" ) . and_return ( "ansible #{ config . version } \n ...\n " )
1285+ end
1286+
1287+ it 'returns the default inventory command' do
1288+ expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
1289+ expect ( args ) . to include ( "--inventory-file=#{ generated_inventory_dir } " )
1290+ } . and_return ( default_execute_result )
1291+ end
1292+ end
1293+
1294+ context 'when ansible version is detected' do
1295+ describe 'version >= 2.19' do
1296+ before do
1297+ config . version = "2.19.0"
1298+ allow ( subject ) . to receive ( :gather_ansible_version ) . with ( "ansible" ) . and_return ( "ansible #{ config . version } \n ...\n " )
1299+ allow ( subject ) . to receive ( :gather_ansible_version ) . with ( "ansible-core" ) . and_return ( "ansible-core #{ config . version } \n ...\n " )
1300+ end
1301+
1302+ it 'returns --inventory as the inventory command' do
1303+ expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
1304+ expect ( args ) . to include ( "--inventory=#{ generated_inventory_dir } " )
1305+ } . and_return ( default_execute_result )
1306+ end
1307+ end
1308+
1309+ describe 'version < 2.19' do
1310+ before do
1311+ config . version = "2.18.5"
1312+ allow ( subject ) . to receive ( :gather_ansible_version ) . with ( "ansible" ) . and_return ( "ansible #{ config . version } \n ...\n " )
1313+ allow ( subject ) . to receive ( :gather_ansible_version ) . with ( "ansible-core" ) . and_return ( "ansible-core #{ config . version } \n ...\n " )
1314+ end
1315+
1316+ it 'returns --inventory-file as the inventory command' do
1317+ expect ( Vagrant ::Util ::Subprocess ) . to receive ( :execute ) . with ( 'ansible-playbook' , any_args ) { |*args |
1318+ expect ( args ) . to include ( "--inventory-file=#{ generated_inventory_dir } " )
1319+ } . and_return ( default_execute_result )
1320+ end
1321+ end
1322+ end
1323+ end
1324+
12651325 end
12661326end
0 commit comments