天道酬勤,学无止境

ansible-facts

Ansible read multiple variables with same name from vars_file

In my ~/ip_vars_file , I have ip : 10.20.30 ip : 10.20.31 ip : 10.20.32 This is created with lineinfile, lineinfile: line="ip{{':'}} {{item.public_ip}}" dest="{{ansible_env.HOME}}/ip_vars_file}}" with_items: servers.tagged_instances #servers is registered in previous task I am unable to read all three ips as with_items. I get only the last IP in my playbook. --- - hosts: localhost tasks: - name: print ips debug: var: item with_items: "{{ ip }}" vars_files: - ~/ip_vars_file The output I am getting is, TASK [print ips] *************************************************************** ok:

2021-09-24 02:48:12    分类:问答    ansible   ansible-2.x   ansible-inventory   ansible-facts

Ansible, How to modify a variable during a loop?

I am using Ansible 2.3.0.0 and I have tested in Ansible 2.4.0.0, obtaining the same result. My problem is simple. I have the following list: vars: password_text_to_encrypt: - { line: "{{truststore_pass }}" , result: } - { line: "{{ keystore_pass }}" , result: } - { line: "{{ gp_pass }}" , result: } - { line: "{{ datasource_password }}" , result: } - { line: "{{ server_password }}" , result: } - { line: "{{ sftp_password }}" , result: } - { line: "{{ db_userpassword }}" , result: } roles: - basic_role I want to encrypt the line value for every item of the list and save it in its results

2021-09-07 00:54:44    分类:问答    ansible   ansible-2.x   ansible-facts

ansible output printing unwanted things. how to format and display only specific data's

I am using ansible 2.4 in centos, trying to run the below script in remote servers and getting the output. Here the problem is yum info output is showing with json format also. But i need to display only the output. How to remove the json format. --- - hosts: GeneralServer tasks: - name: Checking the service status shell: systemctl status {{ item }} with_items: - httpd - crond - postfix - sshd register: service - debug: var=service - name: Checking the package info shell : yum info {{ item }} with_items: - httpd - postfix register: info - debug: var=info - name: Executing the mysql running

2021-09-04 20:22:53    分类:问答    ansible   ansible-2.x   ansible-inventory   ansible-facts   ansible-template

How to Club multiple ansible condition into one based on the set_facts

While going through one of the SO post for ansible recipe, I got it interesting while using set_fact with different conditional checks. However, I answered on the post based on my approach However, I still see there can be an improvement to club multiple conditions for similar task like determine the location resource group and vnet. Below is the playbook and variables file azure_vars.yml Play --- - name: create azure vm hosts: localhost connection: local tasks: - include_vars: azure_vars.yml - set_fact: host: "{{ azure_vm_name.split('.') }}" - set_fact: domain: "{{ host.1 }}.{{ host.2 }}" -

2021-09-04 00:20:59    分类:问答    azure   ansible-2.x   ansible-facts

How to use a dictionary of registered ansible variables in vars?

I want to pass multiple variables to a task using vars. Currently, I am doing it like below vars: var1_name: "var1_value" var2_name: "var2_value" As the number of variables can grow in size, I'd rather prefer to pass the dictionary of variables to the task using vars. I have constructed a dictionary of variables like below - name: set fact hosts: localhost tasks: - set_fact: variables: "{{ variables|default({}) | combine( {item.variable: item.value} ) }}" with_items: - variable: var1_name value: "var1_value" - variable: var2_name value: "var2_name" Dictionary looks something like this:

2021-07-12 02:14:58    分类:问答    ansible   ansible-facts

How to use return values of one task in another task for a different host in ansible

I was trying to setup mysql master slave replication with ansible for a hostgroup consisting of 2 mysql hosts. Here is my scenario: I run one task in the 1st host and skips the 2nd host, so the 1st task (i.e master replication status) returns some values like Position, File etc. Then, I run another task in 2nd host (skips the 1st hosts), This task uses the return values of the 1st task like master.Position, master.File etc. Now, when I run the playbook, the variables of the 1st task does not seem to be working in the 2nd task Inventory File [mysql] stagmysql01 ansible_host=1.1.1.1 ansible_ssh

2021-07-01 10:57:08    分类:问答    mysql   ansible   ansible-2.x   ansible-facts

How to gather facts about disks using Ansible

I am trying to write an Ansible playbook which will identify newly added disks on a RHEL machine. The plan is to run the playbook and cache the disks at that point in time as a fact prior to creating the new disks. After creating the new disks, the the same playbook would be run again and would compute the difference in disks before and after the disks are created. For example, lsblk initially returns the following: NAME SIZE TYPE sda 100G disk ├─sda1 1G part └─sda2 99G part ├─rhel-root 50G lvm ├─rhel-swap 7.9G lvm └─rhel-home 41.1G lvm sr0 1024M rom after adding 8 new disks, lsblk returns

2021-07-01 10:05:36    分类:问答    ansible   ansible-facts

如何在 Ansible 中将命令输出存储到数组中?(How to store command output into array in Ansible?)

问题 本质上,我希望能够使用 ansible 在 Linux 中处理“通配符文件名”。 本质上,这意味着使用带有部分文件名后跟“*”的 ls 命令,以便它仅列出某些文件。 但是,我无法将输出正确存储在变量中,因为返回的文件名可能不止一个。 因此,无论在一项任务中数组中可能有多少个结果,我都希望能够存储这些结果。 然后我希望能够在以后的任务中从数组中检索所有结果。 此外,由于我不知道可能返回多少个文件,我无法为每个文件名执行任务,而数组更有意义。 这背后的原因是随机存储位置中的文件经常更改,但它们始终具有相同的前半部分。 他们名字的后半部分是随机的,我根本不想把它硬编码成 ansible。 我完全不确定如何在 ansible 中正确实现/操作数组,因此以下代码是我“尝试”完成的示例。 显然,如果返回多个文件名,它将无法按预期运行,这就是我寻求有关此主题的帮助的原因: - hosts: <randomservername> remote_user: remoteguy become: yes become_method: sudo vars: aaaa: b tasks: - name: Copy over all random file contents from directory on control node to target clients. This is to show

2021-07-01 08:47:13    分类:技术分享    arrays   linux   ansible   ansible-facts

Reading multiple values from an env file with ansible and storing them as facts

I have the following code which reads values from an environment (.env) file and stores them as facts: - name: Read values from environment shell: "source {{ env_path }}; echo $DB_PASSWORD" register: output args: executable: /bin/bash changed_when: false - name: Store read password set_fact: db_password: "{{ output.stdout }}" when: - db_password is undefined changed_when: false - name: Read values from environment shell: "source {{ env_path }}; echo $DB_USER" register: output args: executable: /bin/bash changed_when: false - name: Store read user set_fact: db_user: "{{ output.stdout }}" when:

2021-06-26 07:28:13    分类:问答    environment-variables   ansible   ansible-facts

Ansible:获取当前目标主机的 IP 地址(Ansible: get current target host's IP address)

问题 如何在角色中获取当前主机的 IP 地址? 我知道您可以获得主机所属的组列表和主机的主机名,但我找不到获取 IP 地址的解决方案。 您可以使用{{inventory_hostname}}获取主机名,使用{{group_names}} 我试过{{ hostvars[{{ inventory_hostname }}]['ansible_ssh_host'] }}和ip="{{ hostvars.{{ inventory_hostname }}.ansible_ssh_host }}" 回答1 所有地址的列表存储在一个事实ansible_all_ipv4_addresses ,在默认地址ansible_default_ipv4.address 。 --- - hosts: localhost connection: local tasks: - debug: var=ansible_all_ipv4_addresses - debug: var=ansible_default_ipv4.address 然后有分配给每个网络接口的地址...在这种情况下,您可以显示所有事实并找到具有您想要使用的值的那个。 回答2 您可以从hostvars 、 dict ansible_default_ipv4和 key address获取 IP address hostvars[inventory

2021-06-25 09:39:24    分类:技术分享    ansible   ansible-facts   ansible-template