'How To create a playbook that copy files to newly provisioned vm and execute those files after adding dynamic hosts to inventory using add_host
I am using Ansible version 2.9.27 and python 2.7.5
I have created a role vmwareinfo where I have assigned tasks in main.yml.
There are two files in the directory:
- create_vsphere_vmware.yml
- Folder with a role named vmwareinfo (which have several subfolders like defaults, files, handlers, meta, _tasks-, etc.).
create_vsphere_vmware.yml
---
- name: create vm in vmware
hosts: 172.17.149.66
become: yes
become_method: sudo
gather_facts: true
roles:
- vmwareinfo
main.yml which is present inside tasks folder
---
- name: Gathering VM info
vmware_guest:
hostname: 172.16.8.16
username: "[email protected]"
password: "pwd8"
validate_certs: False
name: tomcat-dev2
register: var
- debug:
msg:
- " {{ ansible_all_ipv4_addresses[1] }} "
- add_host: name="tomcat-dev2" ansible_ssh_host="{{ansible_all_ipv4_addresses[1]}}" ansible_ssh_pass="pwd8" groups=dynamic_hosts
Now how can I make use of this newly added hosts and execute script on the same? When I am creating a new playbook and giving hosts as dynamic_hosts it says: skipping: no hosts matched.
Below is my new playbook which I am executing after getting new hosts groups as: dynamic_hosts.
execute_script_new_vm.yml
---
- name: create vm vmware
hosts: dynamic_hosts
become: yes
become_method: sudo
gather_facts: true
roles:
- executescript
Now, I created a Role named executescript and inside that tasks folder, I wrote main.yml as below:
---
- name: move over public key
authorized_key: user=foo key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
- name: copy over script files
copy: src=/home/admin/devops/scripts/{{item}} dest=/root/{{items}}
with_items:
- simple.sh
- name: run script files
shell: "/root/{{item}}"
with_items:
- simple.sh
My question is: how can I include this new hosts which I created as dynamic_hosts. How can I execute script on the new provisioned VM? How can I include new hosts inside playbook? Will I have to make one playbook where both hosts (current node and remote node) are present?
Finally I clubbed all and made it in one playbook as the below main.yml
---
- name: Gathering VM info
vmware_guest:
hostname: xxx.xx.x.xx
username: "[email protected]"
password: "pwd8"
validate_certs: False
name: tomcat-dev2
register: var
- debug:
msg:
- " {{ ansible_all_ipv4_addresses[1] }} "
- add_host: name="tomcat-dev2" ansible_ssh_host="{{ansible_all_ipv4_addresses[1]}}" ansible_ssh_pass="pwd8" groups=dynamic_hosts
- hosts: dynamic_hosts
tasks:
- name: move over public key
authorized_key: user=foo key="{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
- name: copy over script files
copy: src=/home/admin/devops/scripts/{{item}} dest=/root/{{items}}
with_items:
- simple.sh
- name: run script files
shell: "/root/{{item}}"
with_items:
- simple.sh
How can I create a playbook that copies files to newly provisioned vm and execute those files after adding dynamic hosts to inventory using add_host? Can't I make this in one playbook OR do I have to write separate one? How can I make use of the fetched IP address?
Solution 1:[1]
As a start, you should spend a bit more time in the Ansible documentation to understand the difference between a playbook (that your run with ansible-playbook) and a task file (that you create inside a role or that you will include with (import|include)_tasks) and that you cannot mix both syntax in a single file. Basically, a task file is a list of task that would go in a play's tasks key.
I will not try to solve your above examples as they contain too many problems to be addressed in one answer. I will not answer either the plethora of questions since they all come down to a single one in my understanding: How do you add a host to in-memory inventory during playbook execution and later play tasks on that target inside a single playbook?
Below is the overall idea: do some tasks where you will recover some information, add some host(s) to the in-memory inventory, and finally run some other tasks on that/those new host(s).
My example will work out of the box anywhere as it is not making any network connection anywhere. The key is to use different plays inside your playbook. I am targeting localhost for my first play but in real life, you can run tasks on any host that is available in your initial inventory.
test-playbook.yml
---
- name: "Play #1 Do some task to gather information"
hosts: localhost
gather_facts: false
tasks:
- name: Some dummy task
set_fact:
gathered_ip: 1.2.3.4
gathered_name: my_host
- name: Add a new host to in-memory inventory in 'dynamic_hosts' group
add_host:
name: "{{ gathered_name }}"
group: dynamic_hosts
ansible_host: "{{ gathered_ip }}"
ansible_password: "v3rys3cr3t"
- name: "Play #2 Do some tasks on the created host(s)"
hosts: dynamic_hosts
gather_facts: false
tasks:
- name: Show some vars from hosts
debug:
msg:
- This host is known as {{ inventory_hostname }}
- The host address is {{ ansible_host }}
which gives:
$ ansible-playbook test-playbook.yml
PLAY [Play #1 Do some task to gather information] ******************************************************************************************************************************************************
TASK [Some dummy task] *******************************************************************************************************************************************
ok: [localhost]
TASK [Add a new host to in-memory inventory in 'dynamic_hosts' group] ********************************************************************************************
changed: [localhost]
PLAY [Play #2 Do some tasks on the created host(s)] ******************************************************************************************************************************************************
TASK [Show some vars from hosts] *********************************************************************************************************************************
ok: [my_host] => {
"msg": [
"This host is known as my_host",
"The host address is 1.2.3.4"
]
}
PLAY RECAP *******************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
my_host : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | β.εηοιτ.βε |
