Set hosts value with an extra-vars doesn't work for my Ansible role









up vote
0
down vote

favorite












I've got a problem with my ansible playbook :



My playbook is composed of 5 roles and looks like this :



---
- name: Création de la machine virtuelle Host_Name à l'aide du template vcenter_template
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/deploy
tags: deploy

- name: Configuration de l'échange de clé de root@lrtstfpe vers root@ Host_Name
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/pre_config
tags: pre_config

- name: Opérations post install
hosts: ' Host_Name '
gather_facts: no
roles:
- roles/VMWare/post_install
tags: post_install

- name: Ajout du pointeur DNS sur le controleur de domaine wpsrvctldom1.uem.lan
hosts: DNS
gather_facts: no
roles:
- roles/DNS/
tags: DNS

- name: Ajout du mot de passe Keepass de l'utilisateur root de la machine
hosts: DNS
gather_facts: no
roles:
- roles/Keepass/
tags: keepass


I launch this playbook with this command :



ansible-playbook playbook/deployVM.yml --extra-vars "Host_Name=toto"


My variable is correctly interpreted in my tasks name but my third role which use the value of this variable to do operations on the appropriate host is skipped and Ansible says "skipping: no hosts matched".



But when I re-launch my playbook with "--tags post_install" to play only this role... It works !



I don't know why when I launch the entire playbook it doesn't work and when I launch just the 3rd role.. It works..



Can someone help me ?



Thx :)



PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role :



[VM]
toto









share|improve this question























  • "PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
    – Matthew L Daniel
    Nov 12 at 0:04














up vote
0
down vote

favorite












I've got a problem with my ansible playbook :



My playbook is composed of 5 roles and looks like this :



---
- name: Création de la machine virtuelle Host_Name à l'aide du template vcenter_template
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/deploy
tags: deploy

- name: Configuration de l'échange de clé de root@lrtstfpe vers root@ Host_Name
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/pre_config
tags: pre_config

- name: Opérations post install
hosts: ' Host_Name '
gather_facts: no
roles:
- roles/VMWare/post_install
tags: post_install

- name: Ajout du pointeur DNS sur le controleur de domaine wpsrvctldom1.uem.lan
hosts: DNS
gather_facts: no
roles:
- roles/DNS/
tags: DNS

- name: Ajout du mot de passe Keepass de l'utilisateur root de la machine
hosts: DNS
gather_facts: no
roles:
- roles/Keepass/
tags: keepass


I launch this playbook with this command :



ansible-playbook playbook/deployVM.yml --extra-vars "Host_Name=toto"


My variable is correctly interpreted in my tasks name but my third role which use the value of this variable to do operations on the appropriate host is skipped and Ansible says "skipping: no hosts matched".



But when I re-launch my playbook with "--tags post_install" to play only this role... It works !



I don't know why when I launch the entire playbook it doesn't work and when I launch just the 3rd role.. It works..



Can someone help me ?



Thx :)



PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role :



[VM]
toto









share|improve this question























  • "PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
    – Matthew L Daniel
    Nov 12 at 0:04












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've got a problem with my ansible playbook :



My playbook is composed of 5 roles and looks like this :



---
- name: Création de la machine virtuelle Host_Name à l'aide du template vcenter_template
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/deploy
tags: deploy

- name: Configuration de l'échange de clé de root@lrtstfpe vers root@ Host_Name
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/pre_config
tags: pre_config

- name: Opérations post install
hosts: ' Host_Name '
gather_facts: no
roles:
- roles/VMWare/post_install
tags: post_install

- name: Ajout du pointeur DNS sur le controleur de domaine wpsrvctldom1.uem.lan
hosts: DNS
gather_facts: no
roles:
- roles/DNS/
tags: DNS

- name: Ajout du mot de passe Keepass de l'utilisateur root de la machine
hosts: DNS
gather_facts: no
roles:
- roles/Keepass/
tags: keepass


I launch this playbook with this command :



ansible-playbook playbook/deployVM.yml --extra-vars "Host_Name=toto"


My variable is correctly interpreted in my tasks name but my third role which use the value of this variable to do operations on the appropriate host is skipped and Ansible says "skipping: no hosts matched".



But when I re-launch my playbook with "--tags post_install" to play only this role... It works !



I don't know why when I launch the entire playbook it doesn't work and when I launch just the 3rd role.. It works..



Can someone help me ?



Thx :)



PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role :



[VM]
toto









share|improve this question















I've got a problem with my ansible playbook :



My playbook is composed of 5 roles and looks like this :



---
- name: Création de la machine virtuelle Host_Name à l'aide du template vcenter_template
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/deploy
tags: deploy

- name: Configuration de l'échange de clé de root@lrtstfpe vers root@ Host_Name
hosts: localhost
gather_facts: no
roles:
- roles/VMWare/pre_config
tags: pre_config

- name: Opérations post install
hosts: ' Host_Name '
gather_facts: no
roles:
- roles/VMWare/post_install
tags: post_install

- name: Ajout du pointeur DNS sur le controleur de domaine wpsrvctldom1.uem.lan
hosts: DNS
gather_facts: no
roles:
- roles/DNS/
tags: DNS

- name: Ajout du mot de passe Keepass de l'utilisateur root de la machine
hosts: DNS
gather_facts: no
roles:
- roles/Keepass/
tags: keepass


I launch this playbook with this command :



ansible-playbook playbook/deployVM.yml --extra-vars "Host_Name=toto"


My variable is correctly interpreted in my tasks name but my third role which use the value of this variable to do operations on the appropriate host is skipped and Ansible says "skipping: no hosts matched".



But when I re-launch my playbook with "--tags post_install" to play only this role... It works !



I don't know why when I launch the entire playbook it doesn't work and when I launch just the 3rd role.. It works..



Can someone help me ?



Thx :)



PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role :



[VM]
toto






ansible ansible-2.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:05

























asked Nov 11 at 16:48









Wrest

105




105











  • "PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
    – Matthew L Daniel
    Nov 12 at 0:04
















  • "PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
    – Matthew L Daniel
    Nov 12 at 0:04















"PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
– Matthew L Daniel
Nov 12 at 0:04




"PS : I add my Host_Name machine into /etc/ansible/hosts file thanks to the 2nd role" does that mean before that 2nd role runs, the toto hostname isn't known to ansible? Because if so, I think you'll need to also use add_host: to teach the in-memory ansible about the host
– Matthew L Daniel
Nov 12 at 0:04












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Ansible reads the /etc/ansible/hosts or any other inventory file before starting the playbook. It is not read again during(or between) the plays. You are populating the hosts' file during the play and then trying to read in the next play; which will not work. You have to add the dynamically created host using add_host to playbook's in-memory inventory.



If you don't want to explicitly add_host then there is another way to refresh inventory. You have to invoke meta module to refresh inventory before the play which requires the updated inventory.



Example:



- name: Re-read inventory files
meta: refresh_inventory





share|improve this answer




















  • Thank you @Kanwar Saad, Your example solved my problem !
    – Wrest
    Nov 12 at 9:53










  • Glad that it helped .. can you accept my answer also
    – Kanwar Saad
    Nov 12 at 10:03










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250952%2fset-hosts-value-with-an-extra-vars-doesnt-work-for-my-ansible-role%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Ansible reads the /etc/ansible/hosts or any other inventory file before starting the playbook. It is not read again during(or between) the plays. You are populating the hosts' file during the play and then trying to read in the next play; which will not work. You have to add the dynamically created host using add_host to playbook's in-memory inventory.



If you don't want to explicitly add_host then there is another way to refresh inventory. You have to invoke meta module to refresh inventory before the play which requires the updated inventory.



Example:



- name: Re-read inventory files
meta: refresh_inventory





share|improve this answer




















  • Thank you @Kanwar Saad, Your example solved my problem !
    – Wrest
    Nov 12 at 9:53










  • Glad that it helped .. can you accept my answer also
    – Kanwar Saad
    Nov 12 at 10:03














up vote
0
down vote



accepted










Ansible reads the /etc/ansible/hosts or any other inventory file before starting the playbook. It is not read again during(or between) the plays. You are populating the hosts' file during the play and then trying to read in the next play; which will not work. You have to add the dynamically created host using add_host to playbook's in-memory inventory.



If you don't want to explicitly add_host then there is another way to refresh inventory. You have to invoke meta module to refresh inventory before the play which requires the updated inventory.



Example:



- name: Re-read inventory files
meta: refresh_inventory





share|improve this answer




















  • Thank you @Kanwar Saad, Your example solved my problem !
    – Wrest
    Nov 12 at 9:53










  • Glad that it helped .. can you accept my answer also
    – Kanwar Saad
    Nov 12 at 10:03












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Ansible reads the /etc/ansible/hosts or any other inventory file before starting the playbook. It is not read again during(or between) the plays. You are populating the hosts' file during the play and then trying to read in the next play; which will not work. You have to add the dynamically created host using add_host to playbook's in-memory inventory.



If you don't want to explicitly add_host then there is another way to refresh inventory. You have to invoke meta module to refresh inventory before the play which requires the updated inventory.



Example:



- name: Re-read inventory files
meta: refresh_inventory





share|improve this answer












Ansible reads the /etc/ansible/hosts or any other inventory file before starting the playbook. It is not read again during(or between) the plays. You are populating the hosts' file during the play and then trying to read in the next play; which will not work. You have to add the dynamically created host using add_host to playbook's in-memory inventory.



If you don't want to explicitly add_host then there is another way to refresh inventory. You have to invoke meta module to refresh inventory before the play which requires the updated inventory.



Example:



- name: Re-read inventory files
meta: refresh_inventory






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 8:10









Kanwar Saad

99811122




99811122











  • Thank you @Kanwar Saad, Your example solved my problem !
    – Wrest
    Nov 12 at 9:53










  • Glad that it helped .. can you accept my answer also
    – Kanwar Saad
    Nov 12 at 10:03
















  • Thank you @Kanwar Saad, Your example solved my problem !
    – Wrest
    Nov 12 at 9:53










  • Glad that it helped .. can you accept my answer also
    – Kanwar Saad
    Nov 12 at 10:03















Thank you @Kanwar Saad, Your example solved my problem !
– Wrest
Nov 12 at 9:53




Thank you @Kanwar Saad, Your example solved my problem !
– Wrest
Nov 12 at 9:53












Glad that it helped .. can you accept my answer also
– Kanwar Saad
Nov 12 at 10:03




Glad that it helped .. can you accept my answer also
– Kanwar Saad
Nov 12 at 10:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250952%2fset-hosts-value-with-an-extra-vars-doesnt-work-for-my-ansible-role%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3