ansible-playbook

Is there anyway to run multiple Ansible playbooks as multiple users more efficiently?

可紊 提交于 2019-11-30 23:12:00
Currently my playbook structure is like this: ~/test_ansible_roles ❯❯❯ tree . . ├── checkout_sources │ └── tasks │ └── main.yml ├── install_dependencies │ └── tasks │ └── main.yml ├── make_dirs │ └── tasks │ └── main.yml ├── setup_machine.yml One of the roles that I have is to install dependencies on my box, so for this I need sudo . Because of that all of my other tasks I need to include the stanza: become: yes become_user: my_username Is there a better way to do this ? You can set the become options per: playbook role task Per playbook: - hosts: whatever become: yes become_user: my_username

How do I loop over each line inside a file with ansible?

纵饮孤独 提交于 2019-11-30 22:31:26
问题 I am looking for something that would be similar to with_items: but that would get the list of items from a file instead of having to include it in the playbook file. How can I do this in ansible? 回答1: I managed to find an easy alternative: - debug: msg="{{item}}" with_lines: cat files/branches.txt 回答2: Lets say you have a file like item 1 item 2 item 3 And you want to install these items.Simply get the file contents to a variable using register.And use this variable for with_items . Make

How can Ansible “register” in a variable the result of including a playbook?

纵饮孤独 提交于 2019-11-30 22:15:31
问题 How can an Ansible playbook register in a variable the result of including another playbook? For example, would the following register the result of executing tasks/foo.yml in result_of_foo ? tasks: - include: tasks/foo.yml - register: result_of_foo How else can Ansible record the result of a task sequence? 回答1: The short answer is that this can't be done. The register statement is used to store the output of a single task into a variable. The exact contents of the registered variable can

Apply with_items on multiple tasks

一世执手 提交于 2019-11-30 17:24:51
Is it possible to apply a list of items to multiple tasks in an Ansible playbook? To give an example: - name: download and execute hosts: server1 tasks: - get_url: url="some-url/{{item}}" dest="/tmp/{{item}}" with_items: - "file1.sh" - "file2.sh" - shell: /tmp/{{item}} >> somelog.txt with_items: - "file1.sh" - "file2.sh" Is there some syntax to avoid the repetition of the item-list? As of today you can use with_items with include , so you'd need to split your playbook into two files: - name: download and execute hosts: server1 tasks: - include: subtasks.yml file={{item}} with_items: - "file1

Ansible lineinfile - modify a line

对着背影说爱祢 提交于 2019-11-30 17:11:42
问题 I'm new to Ansible and trying to modify a line in /etc/default/grub to enable auditing. I need to add audit=1 within the quotes somewhere on a line that looks like: GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap biosdevname=0 net.ifnames=0 rhgb quiet net.ifnames=0" So far I've managed to delete the line and am only left with net.ifnames=0, audit=1 when I use something like lineinfile: state: present dest: /etc/default/grub backrefs: yes regexp: "net.ifnames=0

Apply with_items on multiple tasks

╄→尐↘猪︶ㄣ 提交于 2019-11-30 16:38:20
问题 Is it possible to apply a list of items to multiple tasks in an Ansible playbook? To give an example: - name: download and execute hosts: server1 tasks: - get_url: url="some-url/{{item}}" dest="/tmp/{{item}}" with_items: - "file1.sh" - "file2.sh" - shell: /tmp/{{item}} >> somelog.txt with_items: - "file1.sh" - "file2.sh" Is there some syntax to avoid the repetition of the item-list? 回答1: As of today you can use with_items with include , so you'd need to split your playbook into two files: -

Ansible loop over variables

混江龙づ霸主 提交于 2019-11-30 13:41:42
i am using ansible to update configuration file of newly added NIC for that i have defined some variables in separate yml file /tmp/ip.yml #first interface interface1: eth1 bootproto1: static ipaddress1: 192.168.211.249 netmask1: 255.255.255.0 gateway: 192.168.211.2 DNS1: 192.168.211.2 #second interface interface2: eth2 bootproto2: static ipaddress2: 10.0.0.100 netmask2: 255.0.0.0 Playbook - include_vars: /tmp/ip.yml - name: configuring interface lineinfile: state=present create=yes dest=/etc/sysconfig/network-scripts/ifcfg-{{interface1}} regexp="{{ item.regexp }}" line="{{ item.line }}" with

One loop over multiple Ansible tasks

北城以北 提交于 2019-11-30 12:32:42
问题 I've created an Ansible playbook that creates a cloud instance and then installs some programs on the instance. I want to run this playbook multiple times (without using a bash script). Is it possible to use a loop to loop over those two tasks together (I.E. One loop for two tasks?). All I've been able to find so far is one loop for each individual task 回答1: No that's currently not possible. with_items used to work with the include statement in previous versions of Ansible but was

Write variable to a file in Ansible

天涯浪子 提交于 2019-11-30 10:52:20
问题 I am pulling JSON via the URI module and want to write the received content out to a file. I am able to get the content and output it to the debugger so I know the content has been received, but I do not know the best practice for writing files. 回答1: You could use the copy module, with the content parameter: - copy: content="{{ your_json_feed }}" dest=/path/to/destination/file The docs here: copy module 回答2: Unless you are writing very small files, you should probably use templates. Example:

How To create Ansible variable from string

江枫思渺然 提交于 2019-11-30 07:38:45
问题 For Example: I Have Variable {{ ami_redhat_7_2 }} That I want to use vars: OsType: redhat OsVersion: '7_2' tasks: - debug: 'msg="{{ ami_{{OsType}}_{{ OsVersion }} }}"' I got Error: fatal: [localhost]: FAILED! => { "failed": true, "msg": "template error while templating string: expected token 'end of print statement', got '{'. String: {{ ami_{{ OsType }}_{{ OsVersion }} }}" } 回答1: 'root' variables with dynamic names is a tricky thing in Ansible. If they are host facts, you can access them like